gpt4 book ai didi

c# - 使用 EF 6 克隆实体,空引用异常

转载 作者:太空狗 更新时间:2023-10-29 21:12:42 27 4
gpt4 key购买 nike

我正在尝试使用此处的技术克隆一个实体(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 看起来很正常,没有什么不应该的。当然,_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:

我在这里错过了什么?

最佳答案

我得到了这个工作。诀窍是在检索实体之前设置 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;

我不能说我完全理解这是为什么,但我想这与启用 ProxyCreation 时 EF 执行的延迟加载有关(我在这里不需要/不需要,因为我正在显式加载相关实体).

关于c# - 使用 EF 6 克隆实体,空引用异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27209807/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com