1

我有一个复杂的对象,它引用了其他对象,我试图从我的 Web 应用程序中保存它。

因此,在我的 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");
4

1 回答 1

0

异常表明您的实体或根实体的导航实体已附加到另一个上下文。根据您的架构,您应该创建一个新的上下文并每次都附加它;或者,如果您正在重用上下文,您应该使用新值刷新上下文。

在您的情况下,您似乎正在创建一个新上下文,但尚未处理以前的上下文,因此该实体仍附加到旧上下文。在这种情况下,只需重用旧上下文而不是创建新上下文。

于 2012-05-22T08:10:42.467 回答