我有下面提到的 POCO 课程。
public class AppointmentModel
{
public InvoiceDetail InvoiceDetails { get; set; }
public Invoice Invoice { get; set; }
}
public class InvoiceDetail
{
public Invoice Invoice { get; set; }
}
public class Invoice
{
public Invoice()
{
Id = Guid.NewGuid(); Created = DateTime.Now;
}
public Guid Id { get; set; }
public virtual Appointment Appointment { get; set; }
}
我试图在存储库中添加该模型,如下所示。
public void Booking(AppointmentModel appointmentModel)
{
appointmentModel.InvoiceDetails.Invoice.LatestTotal = latestinvoiceTotal;
Catalog.Appointments.Add(appointmentModel.InvoiceDetails.Invoice.Appointment);
Catalog.SaveChanges();
}
它给出了下面提到的错误。
一个实体对象不能被多个 IEntityChangeTracker 实例引用
堆栈跟踪如下。
at System.Data.Objects.ObjectContext.VerifyContextForAddOrAttach(IEntityWrapper wrappedEntity)
at System.Data.Objects.ObjectContext.AddSingleObject(EntitySet entitySet, IEntityWrapper wrappedEntity, String argumentName)
at System.Data.Objects.DataClasses.RelatedEnd.AddEntityToObjectStateManager(IEntityWrapper wrappedEntity, Boolean doAttach)
at System.Data.Objects.DataClasses.RelatedEnd.AddGraphToObjectStateManager(IEntityWrapper wrappedEntity, Boolean relationshipAlreadyExists, Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Objects.DataClasses.RelatedEnd.IncludeEntity(IEntityWrapper wrappedEntity, Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Objects.DataClasses.EntityReference`1.Include(Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Objects.DataClasses.RelationshipManager.AddRelatedEntitiesToObjectStateManager(Boolean doAttach)
at System.Data.Objects.ObjectContext.AddObject(String entitySetName, Object entity)
at System.Data.Entity.Internal.Linq.InternalSet`1.<>c__DisplayClass5.<Add>b__4()
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 PawLoyalty.Data.PetBookings.Repositories.InvoiceRepository.Booking(AppointmentModel appointmentModel) in d:\PawLoyalty Module\New Booking Flow\NewBookingFlow\PawLoyalty\PawLoyalty.Data\PetBookings\Repositories\InvoiceRepository.cs:line 339
at PawLoyalty.Business.Invoices.InvoiceService.Booking(AppointmentModel appointmentModel) in d:\PawLoyalty Module\New Booking Flow\NewBookingFlow\PawLoyalty\PawLoyalty.Business\Invoices\InvoiceService.cs:line 38
at PawLoyalty.Business.BookingFacadeService.Booking(AppointmentModel appointmentModel) in d:\PawLoyalty Module\New Booking Flow\NewBookingFlow\PawLoyalty\PawLoyalty.Business\BookingFacadeService.cs:line 152
at PawLoyalty.Web.Controllers.PetBookingController.BookingProcess(String providerKey, String ownerKey, String serviceId, String petKeys, String selectedDates, String selectedExtraServices, String selectedResourceId, String key, String type) in d:\PawLoyalty Module\New Booking Flow\NewBookingFlow\PawLoyalty\PawLoyalty.Web\Controllers\PetBookingController.cs:line 243
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
更新
它来自 BasicRepository 如下所示。所有存储库都继承自此。
public class BasicRepository : IBasicRepository
{
private DataCatalog catalog;
protected DataCatalog Catalog { get { if (catalog == null) catalog = new DataCatalog(); return catalog; } }
public void Dispose() { if (catalog != null) catalog.Dispose(); }
}
我可以帮忙吗?