我有一個複雜的對象引用了其他對象,我試圖從我的web應用程序保存它。unitofwork和repositorypattern一個實體對象不能被IEntityChangeTracker的多個實例引用
因此,在我的web應用程序中,我稱之爲業務邏輯,並從bl中調用DAL Facade,然後調用DAL。
當我執行Insert方法時,我遇到了異常。
DAL層的代碼,這是引發異常的地方。
public void InsertEcoBonusRequest(EcoBonusRequest ecoBonusRequest)
{
_context.EcoBonusRequests.Add(ecoBonusRequest);
}
代碼在Unitofwork.cs
public void Save()
{
_context.SaveChanges();
}
private RequestBaseRepository _requestBaseRepository;
public EcoBonusRequestRepository EcoBonusRequestRepository
{
get
{
return _ecobonusworkflowRepository ??
(_ecobonusworkflowRepository = new EcoBonusRequestRepository(_context));
}
}
代碼中的dal門面:
private static readonly UnitOfWork UnitOfWork = new UnitOfWork();
public void Save(EcoBonusRequest ecoBonusRequest)
{
UnitOfWork.EcoBonusRequestRepository.InsertEcoBonusRequest(ecoBonusRequest);
}
代碼在BL
public void Save(EcoBonusRequest ecoBonusRequest)
{
EcoBonusRequestFacade.Save(ecoBonusRequest);
}
**Code in the page.aspx.cs, as you can see the element ecobonusworkflow is neverstored in the database, but the navigation entities are stored already.**
var dealer = DealerBL.GetDealer(Nuteres);
var requester = RequesterBL.GetRequester(int.Parse(DdlRequester.SelectedItem.Value));
var selectedVehicle = VehicleBL.GetVehicle(VehicleInformationControl.Chassis);
var ecoBonusWorkflow = new EcoBonusRequest
{
IsOnHold = true,
AmmountWithoutVAT = decimal.Parse(TxtVatExcluded.Text),
AmmountWithVAT = decimal.Parse(TxtVatIncluded.Text),
BrandReturnedVehicle = TxtBrandReturnedVehicle.Text,
ChassisReturnedVehicle = TxtChasisReturnedVehicle.Text,
Percentage = (decimal.Parse(TxtVatExcluded.Text)/VehicleInformationControl.InvoicedAmmount)*100,
Comments = TxtComments.Text,
Customer = new Customer
{
FirstName = TxtFirstName.Text,
LastName = TxtLastName.Text,
Address = new Address
{
Street = TxtStreet.Text,
Number = TxtNumber.Text,
Box = TxtBox.Text,
City = TxtCity.Text,
Zip = TxtZip.Text
}
},
CurrentStatus = "Approval1",
Dealer = dealer,
Requester = requester,
VehicleDesctructionDate = DateTime.Parse(TxtVehicleDestructionDateReturnedVehicle.Text, userCulture),
DestructionCertificateNumberReturnedVehicle = TxtDestructionCertificateNumberReturnedVehicle.Text,
TypeReturnedVehicle = TxtTypeReturnedVehicle.Text,
Vehicle = selectedVehicle
};
EcoBonusRequest = ecoBonusWorkflow;
UploadFiles();
RedirectToPage("~/Requests/confirmation.aspx");
你從哪裏得到'ecoBonusRequest'來保存?它是從數據庫加載的嗎? –
請參閱更新。 –
也許我需要先添加地址實體,然後添加客戶對象,然後添加ecobonus請求對象,否則EF會自動處理這個問題? –