EF7 beta5:嘗試插入子實體時出錯

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

我使用ASP.NET5和Entity Framework 7.0.0-beta 5創建了一個API。

我有如此處所述創建的Model,DbContext和Repository,當我嘗試使用子實體插入數據時,我的app會拋出異常:

“無法將值NULL插入列'Status',表'Company';列不允許空值.INSERT失敗。”

代碼+例外

Status屬性在我的公司實體中不為null,但我不知道為什麼EF無法插入子實體的值。 (在這種情況下,“1”)

這是堆棧跟踪:

   at Microsoft.Data.Entity.Relational.Update.ReaderModificationCommandBatch.Execute(RelationalTransaction transaction, IRelationalTypeMapper typeMapper, DbContext context, ILogger logger)
   at Microsoft.Data.Entity.Relational.Update.BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
   at Microsoft.Data.Entity.Relational.RelationalDataStore.SaveChanges(IReadOnlyList`1 entries)
   at Microsoft.Data.Entity.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList`1 entriesToSave)
   at Microsoft.Data.Entity.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.Data.Entity.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.Data.Entity.DbContext.SaveChanges()
   at **.Data.Repositories.CompanyRepository.Create(Company company) in D:\**.Data\Repositories\CompanyRepository.cs:line 36
   at **.API.Controllers.CompanyController.Create(Company company) in D:\**.API\Controllers\CompanyController.cs:line 48

這是代碼:

的DbContext

   at Microsoft.Data.Entity.Relational.Update.ReaderModificationCommandBatch.Execute(RelationalTransaction transaction, IRelationalTypeMapper typeMapper, DbContext context, ILogger logger)
   at Microsoft.Data.Entity.Relational.Update.BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
   at Microsoft.Data.Entity.Relational.RelationalDataStore.SaveChanges(IReadOnlyList`1 entries)
   at Microsoft.Data.Entity.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList`1 entriesToSave)
   at Microsoft.Data.Entity.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.Data.Entity.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.Data.Entity.DbContext.SaveChanges()
   at **.Data.Repositories.CompanyRepository.Create(Company company) in D:\**.Data\Repositories\CompanyRepository.cs:line 36
   at **.API.Controllers.CompanyController.Create(Company company) in D:\**.API\Controllers\CompanyController.cs:line 48

模型

   at Microsoft.Data.Entity.Relational.Update.ReaderModificationCommandBatch.Execute(RelationalTransaction transaction, IRelationalTypeMapper typeMapper, DbContext context, ILogger logger)
   at Microsoft.Data.Entity.Relational.Update.BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
   at Microsoft.Data.Entity.Relational.RelationalDataStore.SaveChanges(IReadOnlyList`1 entries)
   at Microsoft.Data.Entity.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList`1 entriesToSave)
   at Microsoft.Data.Entity.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.Data.Entity.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.Data.Entity.DbContext.SaveChanges()
   at **.Data.Repositories.CompanyRepository.Create(Company company) in D:\**.Data\Repositories\CompanyRepository.cs:line 36
   at **.API.Controllers.CompanyController.Create(Company company) in D:\**.API\Controllers\CompanyController.cs:line 48

存儲庫 (僅創建方法)

   at Microsoft.Data.Entity.Relational.Update.ReaderModificationCommandBatch.Execute(RelationalTransaction transaction, IRelationalTypeMapper typeMapper, DbContext context, ILogger logger)
   at Microsoft.Data.Entity.Relational.Update.BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
   at Microsoft.Data.Entity.Relational.RelationalDataStore.SaveChanges(IReadOnlyList`1 entries)
   at Microsoft.Data.Entity.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList`1 entriesToSave)
   at Microsoft.Data.Entity.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.Data.Entity.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.Data.Entity.DbContext.SaveChanges()
   at **.Data.Repositories.CompanyRepository.Create(Company company) in D:\**.Data\Repositories\CompanyRepository.cs:line 36
   at **.API.Controllers.CompanyController.Create(Company company) in D:\**.API\Controllers\CompanyController.cs:line 48

有誰知道如何正確插入子實體?

熱門答案

在EF7中,您需要將子對象附加到上下文中。這與EF6不同,我很確定你面臨的情況。在保存之前嘗試將對象附加到dbcontext。

發生在我身上,請查看此問題: https//github.com/aspnet/EntityFramework/issues/2387獲取更多信息。




許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因