您好我需要插入采购订单标题和详细信息。现在将数据插入PurchaseOrder表是成功的,但是当我尝试将数据插入PurchaseOrderDetails时,它将数据插入到许多不同的表中。我试过调试它,但我没有发现问题。我还检查了SQL Profiler,我发现EF正在向ff表中插入数据“ NewProposal,机构,产品,等等。
请帮帮我,如何防止EF将数据插入到其他表中,我只需要EF将数据插入到PO和POdetails表中。
这是我的采购订单模型:
public class PurchaseOrder
{
[Key]
public int PurchaseOrderID {get; set;}
[Required]
public string PurchaseOrderNo { get; set; }
[Required]
public DateTime PurchaseOrderDate { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateModified { get; set; }
public bool isDraft { get; set; }
public int? InstitutionID { get; set; }
public virtual Institution Institution { get; set; }
[Required]
public DateTime ReceivedDate { get; set; }
public string Remarks { get; set; }
public List<PODetail> PODetail { get; set; }
}
以下是采购订单明细模型:
public class PODetail
{
[Key]
public int ID { get; set; }
public virtual PurchaseOrder PurchaseOrder { get; set; }
public virtual ULIV.ViewModels.NewProposal Proposal { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateModified { get; set; }
public bool isDraft { get; set; }
}
供您参考,这是NewProposal VM:
public class NewProposal
{
[Key]
[Display(Name="Proposal ID")]
public int ProposalID { get; set; }
public int InstitutionID { get; set; }
[ForeignKey("InstitutionID")]
public virtual Institution Institution { get; set; }
public string ProposalCode { get; set; }
[Display(Name = "Proposed Unit Price")]
///[Required]
public decimal ProposedUnitPrice { get; set; }
///[Required]
[Display(Name = "Proposed Volume")]
public string ProposedVolume { get; set; }
public string Remarks { get; set; }
public int StatusID { get; set; }
public virtual Status Status { get; set; }
public int VaccineTypeID { get; set; }
public virtual VaccineType VaccineType { get; set; }
public int ProductID { get; set; }
public virtual ProductModel Product {get; set;}
public DateTime DateModified { get; set; }
public DateTime DateCreated { get; set; }
public decimal FinalUnitPrice { get; set; }
public decimal FinalVolume { get; set; }
public decimal FinalTotalAmount { get; set; }
public string RelatedProposalCode { get; set; }
//public List<PODetail> PODetail { get; set; }
public string getdatestring
{
get {
return DateModified.ToShortDateString();
}
}
public bool isDraft { get; set; }
public int UserID { get; set; }
public string DateDisplay { get; set; }
}
最后,这是用于保存采购订单的创建操作:
[HttpPost]
public ActionResult Create(PurchaseOrderViewModel purchaseorderviewmodel)
{
Session["PurchaseOrderNo"] = purchaseorderviewmodel.PurchaseOrderNo;
purchaseorderviewmodel.AddedProposal = (List<ULIV.ViewModels.NewProposal>)Session["AddedProposal"];
int instituionId = Convert.ToInt32(Session["InstitutionID"]);
purchaseorderviewmodel.isDraft = true;
purchaseorderviewmodel.DateCreated = DateTime.Now;
purchaseorderviewmodel.DateModified = DateTime.Now;
if (ModelState.IsValid)
{
try
{
ULIV.Models.PurchaseOrder newPO = new PurchaseOrder();
newPO.Institution = db.Institutions.Find(instituionId);
newPO.PurchaseOrderNo = purchaseorderviewmodel.PurchaseOrderNo;
newPO.PurchaseOrderDate = purchaseorderviewmodel.PurchaseOrderDate;
newPO.ReceivedDate = purchaseorderviewmodel.ReceivedDate;
newPO.Remarks = purchaseorderviewmodel.Remarks;
//newPO.Institution =
newPO.DateCreated = DateTime.Now;
newPO.DateModified = DateTime.Now;
newPO.isDraft = true;
db.PurchaseOrders.Add(newPO);
db.SaveChanges();
var query = (from m in db.PurchaseOrders orderby m.PurchaseOrderID descending select m).Take(1).ToList();
foreach (var d in purchaseorderviewmodel.AddedProposal)
{
PODetail newPODetail = new PODetail();
foreach (var i in query)
{
newPODetail.PurchaseOrder = i;
}
newPODetail.Proposal = d;
newPODetail.DateCreated = DateTime.Now;
newPODetail.DateModified = DateTime.Now;
newPODetail.isDraft = true;
db.PODetails.Add(newPODetail);
db.SaveChanges();
}
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
}
}
}
Session["AddedProposal"] = null;
return RedirectToAction("Index","Fulfillment");
}
ViewBag.Proposal = new SelectList(db.NewProposal.Where(x => x.Status.StatusID == 3), "ProposalID", "ProposalCode");
return View(purchaseorderviewmodel);
}
答案 0 :(得分:0)
你这样做:
newPODetail.Proposal = d;
d
成为ViewModel
的一部分。这意味着您将Proposal设置为不是来自DbContext
的实例。因此,EF认为这是一个新事物(因为它不知道它)并且插入。
你可以做的是使用Proposal
或类似的东西从DbContext
找到实际的db.Proposals.Find(d.Id)
,或者使用db.Attach(d)
将对象附加到当前{ {1}}。
有关附加的更多信息:http://msdn.microsoft.com/en-us/library/system.data.entity.dbset.attach%28v=vs.103%29.aspx