EF Core实体未添加到数据库

时间:2018-05-02 11:53:00

标签: c# asp.net-core entity-framework-core

我的实体未添加到特定的表格中。

虽然其他所有表都运行良好,但我有一个特定的表没有,我找不到原因。在同一个序列中,我在SaveChangeAsync之前有不同的添加/更新,每个查询都可以正常工作,除了一个没有添加实体的表。

我检查过该实体,其数据填写完整。

在对此表中没有数据的原因进行一些调查之后,我使用了即时窗口来执行一些测试,这是我得到的序列:

  1. 从表中算起
  2. 添加实体
  3. 保存DBContext
  4. 从表中算起
  5. 计数

    _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
    

    添加实体

    _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
    

    保存更改

    _DbContext.SaveChanges()
    0
    

    表计数

    _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
    

    正如我们所观察到的,该实体已成功附加,但在保存时,没有任何反应......可能导致这种情况的原因是什么?它被声明为任何其他表。它有一个Guid Id作为密钥,在代码端设置

    其他信息

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

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

    给出一个实体,它具有一组唯一ID和许多其他对这种情况不感兴趣的字段。

    Save之后,Entries.Count返回0,但没有添加任何内容......

    我删除了SQL中可能干扰的任何外键以及实体描述中除主键之外的任何键。

    _DbContext.Attendees.FirstOrDefault()运行SQL语句,并按预期返回null,因为数据库中没有条目。

    如果我做_DbContext.Attendees.Add(new Attendee(){Id=Guid.NewId()})它有效......我会从那里开始......

1 个答案:

答案 0 :(得分:0)

由于在SELECTToListAsync次来电之间有FirstOrDefaultAsync次陈述(AddUPDATE),因此不知怎样。其中一个Add语句在某处丢失了,我找不到原因。

我的解决方案是创建一个新的DbContext,并继续使用更新某些实体所需的DbContextSELECT语句,并将这些SELECT作为第一个移动语句。