我正在尝试使用此处的技术克隆实体(EF 6.x,代码优先):Entity Framework 5 deep copy/clone of an entity
但是,在尝试将克隆的票证添加到DbSet时,我收到一个奇怪的NullReference异常:
var clonedTicket = _context.Tickets
.AsNoTracking()
.Include(o => o.Lines.Select(l => l.Serials))
.Include(o => o.Payments.Select(p => p.PayCodeInfo))
.SingleOrDefault(o => o.TicketId == ticketId);
clonedTicket.TicketId = _context.Tickets.Max(o => o.TicketId) + 1;
foreach (var line in clonedTicket.Lines)
line.TicketId = clonedTicket.TicketId;
foreach (var payment in clonedTicket.Payments)
payment.TicketId = clonedTicket.TicketId;
_context.Tickets.Add(clonedTicket); <---- System.NullReferenceException
_context.SaveChanges();
例外是_context.Tickets.Add(clonedTicket)。
clonedTicket看起来很正常,没有什么是null,不应该是。当然,_context和Tickets(DbSet)都是非空的。
以下是完整的例外情况:
System.NullReferenceException was unhandled by user code
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.MarkForeignKeyPropertiesModified()
at System.Data.Entity.Core.Objects.DataClasses.EntityReference.AddToNavigationPropertyIfCompatible(RelatedEnd otherRelatedEnd)
at System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.IncludeEntity(IEntityWrapper wrappedEntity, Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Entity.Core.Objects.DataClasses.EntityCollection`1.Include(Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Entity.Core.Objects.DataClasses.RelationshipManager.AddRelatedEntitiesToObjectStateManager(Boolean doAttach)
at System.Data.Entity.Core.Objects.ObjectContext.AddObject(String entitySetName, Object entity)
at System.Data.Entity.Internal.Linq.InternalSet`1.<>c__DisplayClassd.<Add>b__c()
at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
at System.Data.Entity.DbSet`1.Add(TEntity entity)
at TheRepo.CreateCloneFromTicket(Int32 ticketId, String clientIp) in c:\...\TheRepo.cs:line 23
at TheController.CreateReturn(Int32 ticketId) in c:\...\TheController.cs:line 232
at lambda_method(Closure , Object , Object[] )
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
InnerException:
我在这里缺少什么?
答案 0 :(得分:8)
我得到了这个工作。诀窍是在检索实体之前设置ProxyCreationEnabled = false:
_context.Configuration.ProxyCreationEnabled = false;
var returnTicket = _context.Tickets
.AsNoTracking()
.Include(o => o.Lines.Select(l => l.Serials))
.Include(o => o.Payments)
.SingleOrDefault(o => o.TicketId == ticketId);
_context.Configuration.ProxyCreationEnabled = true;
我不能说我完全理解为什么会这样,但我想这与EF在启用ProxyCreation时所做的延迟加载有关(我不需要/想要这里,因为我明确加载相关实体)。