2016-12-11 40 views
0

我有三個實體:產品,項目(包括一個產品,並沒有由客戶購買的產品)和因子如下:試圖添加一個實體,而且還插入另一實體DB

public class Factor 
{ 
    [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public virtual int FactorId { get; set; } 
    [Required] 
    public virtual System.Guid TrackingCode { set; get; } 
    [StringLength(1000)] 
    public string StatusOfFactor { set; get; } 
    [Required, DataType(DataType.EmailAddress), StringLength(200)] 
    public string EmailOfFactor { set; get; } 
    // price before discount 
    public virtual double PriceOfFactor { set; get; } 
    public virtual double DiscountOfFactor { set; get; } 
    [StringLength(3000,MinimumLength =0)] 
    public virtual string GeneralDescription { set; get; } 
    public virtual List<Item> Items { set; get; } 
} 


public class Item 
{ 
    //[DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int ItemId { set; get; } 
    public int Count { set; get; } 
    // 1 -> 1 relationship between product and Item 
    public virtual Product Product { set; get; } 
    // * -> * relationship between Item and Factor 
    public virtual List<Factor> Factors { set; get; } 
} 


public class Product 
{ 
    [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public virtual int ProductId { set; get; } 
    [Required] 
    [StringLength(128)] 
    public virtual string NameFa { set; get; } 
    [Required] 
    [Range(0, 990000000)] 
    public virtual double Price { set; get; } 
    // many->many relationship category and product 
    public virtual List<Category> Categories { set; get; } 
    //public virtual List<Factor> Factors { get; set; } 
    public virtual Item Item { set; get; } 
} 

實體之間的關係如下: 1 - > 1的產品,項目和* - > *項因子

public class ProductConfig:EntityTypeConfiguration<Product> 
{ 
    public ProductConfig() { 
     // one to one relationship between product and item 
     HasOptional(p => p.Item).WithRequired(i => i.Product); 
    } 
} 

public class FactorConfig : EntityTypeConfiguration<Factor> 
{ 
    public FactorConfig() { 
     HasMany(p => p.Items) 
     .WithMany(p => p.Factors) 
     .Map(c => { 
      c.ToTable("ItemsFactors"); 
      c.MapLeftKey("FactorID"); 
      c.MapRightKey("ItemID"); 
     }); 
    } 
} 

當我插入一個因子數據庫,另一個產品也添加到產品表! 這裏是插入因子分貝代碼:

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult RegisterFactor([Bind(Include = "FactorId,TrackingCode,RegistrationDate,PaymentSucceed,PaymentIdOfBank,PaymentDate,StatusOfFactor,EmailOfFactor,TranmissionSucceed,TransmissionDate,PriceOfFactor,DiscountOfFactor,GeneralDescription,IsReturned,ReturnedDate,ReturnedDescription")]Factor factor) 
{ 
    { 
     // Session consist of Items and Items consist of Products 
     List<Item> purchasedItems = 
     (List<Item>)Session[Constants.SHOPPINGBAG]; 
     double totalPriceBeforeDiscount = 0.0d; 
     double totalDiscount = 0.0d;    
     double totalPriceAfterDiscount = 0.0; 
     // 
     Guid trackingCode = Guid.NewGuid(); 
     factor.Items = new List<Item>(); 
     if (purchasedItems == null) 
     { 
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
     } 
     foreach (Item item in purchasedItems) 
     { 
      if (item.Product != null) 
      { 
       factor.Items.Add(item); 
      } 
     } 
     // allocate data to factor 
     factor.PriceOfFactor = totalPriceBeforeDiscount; 
     factor.DiscountOfFactor = totalDiscount; 
     factor.PaymentDate = DateTime.Now; 
     factor.RegistrationDate = DateTime.Now; 
     factor.TrackingCode = trackingCode; 
     if (ModelState.IsValid) 
     { 
      //db.Factors.AsNoTracking(); 
      db.Products.AsNoTracking(); 
      db.Factors.Add(factor); 
      db.SaveChanges(); 
      EmptyShoppingCart(); 
      return View("RegisterFactorSucceed", factor); 
     } 
     return View(factor); 
    } 
} 

這實在是尷尬(對我來說)。爲什麼會發生?

回答

0

您的'ProductConfig'表示每當Item有'product'時,則應該將相同的內容添加到Product表中。這是修改後的代碼。

ProductConfig類

public class ProductConfig : EntityTypeConfiguration<Product> 
{ 
    public ProductConfig() 
    { 
     // one to one relationship between product and item 
     HasOptional(p => p.Item).WithOptionalDependent(i => i.Product); 
    } 
} 

如果更改Item.Product爲null,那麼它不會添加到產品表。

foreach (Item item in purchasedItems) 
     { 
      if (item.Product != null) 
      { 
       factor.Items.Add(item); 
       item.Product = null;//Setting Product to null 
      } 
     } 
+0

非常感謝您的回覆。產品類別取決於Item類別。即每個產品必須有一個或多個產品,但對產品來說不是這樣。產品和產品之間的關係是否正確? – Xeta7

0

問題很可能是因爲您的因子對象有一個內存中產品的引用,您將傳回給EF。因此,EF將其解釋爲新產品並將其與該因素一起添加。

您需要可以..

  1. 未通過填充了產品在內存中要添加的因素一起,並使用其鏈接IDS
  2. 確保裝入現有的產品要附加的在保存之前跟蹤數據庫的因素。

我認爲https://msdn.microsoft.com/en-gb/magazine/dn166926.aspx可能會有所幫助。

+0

親愛的grrrrrrrrrrrrr,謝謝你的回覆。我認爲這是由您發送的第一個音符引起的。但我應該如何刪除內存中的填充產品? – Xeta7

+0

您需要查看使用ID來引用EF中的其他實體。他們大量簡化了情況。我認爲https://msdn.microsoft.com/en-gb/magazine/dn166926.aspx會解釋更多。 – gmn

相關問題