I have a complex object which has references to other objects, I am trying to save it from my web application.
so, in my webapplication, I call the business logic, and from the bl, I call the DAL Facade, and then the DAL.
When I execute the Insert method I am having the exception.
Code in the DAL layer, this is where the exception is raised.
public void InsertEcoBonusRequest(EcoBonusRequest ecoBonusRequest)
{
_context.EcoBonusRequests.Add(ecoBonusRequest);
}
Code in the Unitofwork.cs
public void Save()
{
_context.SaveChanges();
}
private RequestBaseRepository _requestBaseRepository;
public EcoBonusRequestRepository EcoBonusRequestRepository
{
get
{
return _ecobonusworkflowRepository ??
(_ecobonusworkflowRepository = new EcoBonusRequestRepository(_context));
}
}
Code in the dal facade:
private static readonly UnitOfWork UnitOfWork = new UnitOfWork();
public void Save(EcoBonusRequest ecoBonusRequest)
{
UnitOfWork.EcoBonusRequestRepository.InsertEcoBonusRequest(ecoBonusRequest);
}
Code in the 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");
The exception states that your entity, or navigation entities of the root entity is already attached to another context.
Depending on your architecture, you should either create a new context and attach it every time; or if you are reusing the context you should refresh the context with new values.
In your case, it seems you are creating a new context, but a previous context has not been disposed thus the entity is still attached to the older context. In this case, just reuse the older context instead of creating a new one.