EF Core entity not added to database

asp.net-core c# entity-framework-core

Question

My entities are not added to a specific table.

While every other tables are working great, I have a specific one which is not and I can't find out why. In a same sequence, I have different add/update before a SaveChangeAsync, every query works fine except for one table where entities are not added.

I have checked the entity, its data is well filled.

After some investigations on why I had no data in this table, I used the immediate window to perform some tests, here is what I get for the sequence:

  1. Count from table
  2. Add an entity
  3. Save DBContext
  4. Count from table

Count

_DbContext.Attendees.Count()
Microsoft.EntityFrameworkCore.Database.Command:Information: Executed DbCommand (17ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT COUNT(*)
FROM [Attendees] AS [e]
Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Message","time":"2018-05-02T11:39:35.5327129Z","tags":{"ai.operation.parentId":"|d06def47-4faba4501b66147c.","ai.operation.id":"d06def47-4faba4501b66147c","ai.location.ip":"127.0.0.1","ai.operation.name":"POST Order/VerifyPayment [cartId]","ai.application.ver":"1.0.0.0","ai.internal.nodeName":"MyPC","ai.cloud.roleInstance":"MyPC","ai.internal.sdkVersion":"aspnet5c:2.1.1"},"data":{"baseType":"MessageData","baseData":{"ver":2,"message":"Executed DbCommand (17ms) [Parameters=[], CommandType='Text', CommandTimeout='30']\r\nSELECT COUNT(*)\r\nFROM [Attendees] AS [e]","severityLevel":"Information","properties":{"{OriginalFormat}":"Executed DbCommand ({elapsed}ms) [Parameters=[{parameters}], CommandType='{commandType}', CommandTimeout='{commandTimeout}']{newLine}{commandText}","elapsed":"17","commandTimeout":"30","commandText":"SELECT COUNT(*)\r\nFROM [Attendees] AS [e]","AspNetCoreEnv
ironment":"Development","commandType":"Text","DeveloperMode":"true","CategoryName":"Microsoft.EntityFrameworkCore.Database.Command"}}}}
0

Add entity

_DbContext.Attendees.Add(attendees[0])
{Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalClrEntityEntry}
    Collections: {System.Linq.Enumerable.WhereSelectEnumerableIterator<Microsoft.EntityFrameworkCore.Metadata.INavigation, Microsoft.EntityFrameworkCore.ChangeTracking.CollectionEntry>}
    Context: {Project.Data.DbContext}
    CurrentValues: {Microsoft.EntityFrameworkCore.ChangeTracking.Internal.CurrentPropertyValues}
    Entity (Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry): {Project.Models.Attendee}
    Entity: {Project.Models.Attendee}
    InternalEntry: {Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalClrEntityEntry}
    IsKeySet: true
    Members: {System.Linq.Enumerable.Concat2Iterator<Microsoft.EntityFrameworkCore.ChangeTracking.MemberEntry>}
    Metadata: {EntityType: Attendee}
    Navigations: {System.Linq.Enumerable.SelectEnumerableIterator<Microsoft.EntityFrameworkCore.Metadata.INavigation, Microsoft.EntityFrameworkCore.ChangeTracking.NavigationEntry>}
    OriginalValues: {Microsoft.EntityFrameworkCore.ChangeTracking.Internal.OriginalPropertyValues}
    Properties: {System.Linq.Enumerable.SelectEnumerableIterator<Microsoft.EntityFrameworkCore.Metadata.IProperty, Microsoft.EntityFrameworkCore.ChangeTracking.PropertyEntry>}
    References: {System.Linq.Enumerable.WhereSelectEnumerableIterator<Microsoft.EntityFrameworkCore.Metadata.INavigation, Microsoft.EntityFrameworkCore.ChangeTracking.ReferenceEntry>}
    State: Added

Save changes

_DbContext.SaveChanges()
0

Table Count

_DbContext.Attendees.Count()
Microsoft.EntityFrameworkCore.Database.Command:Information: Executed DbCommand (19ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT COUNT(*)
FROM [Attendees] AS [e]
Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Message","time":"2018-05-02T11:40:05.8741131Z","tags":{"ai.operation.parentId":"|d06def47-4faba4501b66147c.","ai.operation.id":"d06def47-4faba4501b66147c","ai.location.ip":"127.0.0.1","ai.operation.name":"POST Order/VerifyPayment [cartId]","ai.application.ver":"1.0.0.0","ai.internal.nodeName":"MyPC","ai.cloud.roleInstance":"MyPC","ai.internal.sdkVersion":"aspnet5c:2.1.1"},"data":{"baseType":"MessageData","baseData":{"ver":2,"message":"Executed DbCommand (19ms) [Parameters=[], CommandType='Text', CommandTimeout='30']\r\nSELECT COUNT(*)\r\nFROM [Attendees] AS [e]","severityLevel":"Information","properties":{"{OriginalFormat}":"Executed DbCommand ({elapsed}ms) [Parameters=[{parameters}], CommandType='{commandType}', CommandTimeout='{commandTimeout}']{newLine}{commandText}","elapsed":"19","commandTimeout":"30","commandText":"SELECT COUNT(*)\r\nFROM [Attendees] AS [e]","AspNetCoreEnv
ironment":"Development","commandType":"Text","DeveloperMode":"true","CategoryName":"Microsoft.EntityFrameworkCore.Database.Command"}}}}
0

As we can observe, the entity is attached successfully, but on save, nothing happens... What could cause this? It is declared as any other table. It has a Guid Id as key, which is set on code-side

Additional information

_DbContext.ChangeTracker.Entries<Attendee>().Count() => 1

and

_DbContext.ChangeTracker.Entries<Attendee>().ElementAt(0).Entity

gives an entity, with a set unique id and many other fields which have not much interest in this case.

After a Save, Entries.Count returns 0, but nothing has been added...

I have removed any foreign key in SQL that could interfere and any key besides primary key in the entity description.

_DbContext.Attendees.FirstOrDefault() runs the SQL statement, and returns null as expected as there is no entry in the DB.

If I do _DbContext.Attendees.Add(new Attendee(){Id=Guid.NewId()}) it works... I will start from there...

1
1
5/2/2018 4:59:53 PM

Popular Answer

Somehow it was breaking because there were SELECT statements (ToListAsync and FirstOrDefaultAsync) between Add and UPDATE calls. One of the Add statement got lost somewhere and I couldn't find why.

My solution has been to create a new DbContext, and keep on this DbContext only SELECT statements that were required to update some entities and move these SELECT as first statements.

0
5/2/2018 5:57:00 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow