私は以下の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(); }
}
助けてもらえますか?