实体框架6:添加多个嵌套级别(>3 +)
本文关键字:嵌套 框架 添加 实体 | 更新日期: 2023-09-27 18:13:18
我是新的实体框架。当我阅读本文并成功编写helloworld应用程序时。但是,当设计具有许多嵌套级别的对象时,例如:
- 学校(1 - n)教师
- 教师(1 - n)班
- 班级(1 - n)学生
试图一次添加所有元素,它抛出异常。如果我只添加School - Teacher,那么Teacher - Class…它好。
这里有一个例外:
Object reference not set to an instance of an object.
at System.Data.Entity.Core.Objects.EntityEntry.SetCachedChangingValues(String entityMemberName, Object changingObject, String changingMember, EntityState changingState, Object oldValue)
at System.Data.Entity.Core.Objects.EntityEntry.EntityMemberChanged(String entityMemberName, Object complexObject, String complexObjectMemberName)
at System.Data.Entity.Core.Objects.EntityEntry.EntityMemberChanged(String entityMemberName)
at System.Data.Entity.Core.Objects.ObjectStateEntry.System.Data.Entity.Core.Objects.DataClasses.IEntityChangeTracker.EntityMemberChanged(String entityMemberName)
at System.Data.Entity.Core.Objects.Internal.SnapshotChangeTrackingStrategy.SetCurrentValue(EntityEntry entry, StateManagerMemberMetadata member, Int32 ordinal, Object target, Object value)
at System.Data.Entity.Core.Objects.Internal.EntityWrapper`1.SetCurrentValue(EntityEntry entry, StateManagerMemberMetadata member, Int32 ordinal, Object target, Object value)
at System.Data.Entity.Core.Objects.EntityEntry.SetCurrentEntityValue(StateManagerTypeMetadata metadata, Int32 ordinal, Object userObject, Object newValue)
at System.Data.Entity.Core.Objects.ObjectStateEntryDbUpdatableDataRecord.SetRecordValue(Int32 ordinal, Object value)
at System.Data.Entity.Core.Objects.DbUpdatableDataRecord.SetValue(Int32 ordinal, Object value)
at System.Data.Entity.Core.Mapping.Update.Internal.PropagatorResult.SetServerGenValue(Object value)
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.BackPropagateServerGen(List`1 generatedValues)
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>b__2(UpdateTranslator ut)
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult, Func`2 updateFunction, Boolean throwOnClosedConnection)
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update(Boolean throwOnClosedConnection)
at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__33()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy)
at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass28.<SaveChanges>b__25()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at Repository.UnitOfWork.Save() in d:'Practice'DotNet'RelationShip'Repository'UnitOfWork.cs:line 44
at RelationShip.Program.Main(String[] args) in d:'Practice'DotNet'RelationShip'RelationShip'Program.cs:line 35
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
我知道这是一个非常晚的答案,但今天我得到了同样的错误。这是一个模糊的错误,基本上让你摸不着头脑,没有太多的信息来弄清楚发生了什么。
在我的情况下,我认为这是由于我有一个具有3个主键字段的实体,其中2个来自父表。实体有一个指向父实体的外键属性。
当我插入这些对象时,我通过父对象分配了外键引用,并且我得到了与调用SaveChanges时相同的错误。我必须这样做,因为父对象是新的,我不知道它的主键字段值,直到对象实际保存在数据库中(它的主键是一个标识字段)。
我最终做的是我开始了一个事务,我执行了创建父对象的第一个SaveChanges,然后分别分配了子对象的3个主键字段值(而不是分配父对象+第3个PK字段),然后我在db上下文中调用了第二个SaveChanges,它工作了。