2015-07-13 60 views
0

我正在開發使用ASP.Net MVC 4框架的在線商店。實體框架不保存對象屬性

我有一個對象類別 - 在存放物品的類別(男士服裝 - 例如):

public class Category 
{ 
    [Required] 
    public long id { get; set; } 

    [Required] 
    public string name { get; set; } 

    public bool active { get; set; } 
    public bool displayOnTop { get; set; } 
    public bool showSubcategoriesItems { get; set; } 

    public Category parentCategory { get; set; } 

    public virtual Collection<Item> items { get; set; } 

    public Category() 
    { 
     this.items = new Collection<Item>(); 
    } 
} 

有一個屬性parentCategory - 這意味着這個類別包括到另一個(男士服裝>例如襯衫)。

對於編輯現有的類別I使用了以下行動:

[HttpPost] 
public ActionResult EditCategory(Category editedCategory, string parentCategorySelector) 
{ 
    UsersContext db = new UsersContext(); 

    Category existingCategory = db.categories.SingleOrDefault(c => c.id == editedCategory.id); 
    long parentCategoryId = (long)Int32.Parse(parentCategorySelector); 
    Category parentCategory = db.categories.SingleOrDefault(c => c.id == parentCategoryId); 

    if (existingCategory == null) return RedirectToAction("Index", "Admin"); 

    existingCategory.name = editedCategory.name; 
    existingCategory.active = editedCategory.active; 
    existingCategory.displayOnTop = editedCategory.displayOnTop; 
    existingCategory.showSubcategoriesItems = editedCategory.showSubcategoriesItems; 
    existingCategory.parentCategory = parentCategory; 

    db.Entry(existingCategory.parentCategory).State = EntityState.Modified; 
    db.SaveChanges(); 

    return View(existingCategory); 
} 

一切工作正常,除非我想parentCategory設置爲null - 它設置爲NULL代碼,但空值沒有被保存到數據庫 - 之前的值仍然存在。

但是,如果我不喜歡這樣寫道:

[HttpPost] 
public ActionResult EditCategory(Category editedCategory, string parentCategorySelector) 
{ 
    UsersContext db = new UsersContext(); 

    Category existingCategory = db.categories.SingleOrDefault(c => c.id == editedCategory.id); 
    long parentCategoryId = (long)Int32.Parse(parentCategorySelector); 
    Category parentCategory = db.categories.SingleOrDefault(c => c.id == parentCategoryId); 

    if (existingCategory == null) return RedirectToAction("Index", "Admin"); 

    existingCategory.name = editedCategory.name; 
    existingCategory.active = editedCategory.active; 
    existingCategory.displayOnTop = editedCategory.displayOnTop; 
    existingCategory.showSubcategoriesItems = editedCategory.showSubcategoriesItems; 
    existingCategory.parentCategory = null; 

    db.Entry(existingCategory.parentCategory).State = EntityState.Modified; 
    db.SaveChanges(); 

    return View(existingCategory); 
} 

即我將它設置爲null無條件,它被正確保存。

我在做什麼錯?

回答

1

爲什麼在類別下沒有Key屬性。 id?如果id是主鍵,它必須在那裏。

您應該使用包括load relate entities

[HttpPost] 
    public ActionResult EditCategory(Category editedCategory, string parentCategorySelector) 
    { 
     using(UsersContext db = new UsersContext()) 
     { 
      Category existingCategory = db.categories.Include(c => c.parentCategory).SingleOrDefault(c => c.id == editedCategory.id); 
      long parentCategoryId = (long)Int32.Parse(parentCategorySelector); 
      Category parentCategory = db.categories.SingleOrDefault(c => c.id == parentCategoryId); 

      if (existingCategory == null) return RedirectToAction("Index", "Admin"); 

      existingCategory.name = editedCategory.name; 
      existingCategory.active = editedCategory.active; 
      existingCategory.displayOnTop = editedCategory.displayOnTop; 
      existingCategory.showSubcategoriesItems = editedCategory.showSubcategoriesItems; 
      existingCategory.parentCategory = parentCategory; 

      db.SaveChanges(); 

      return View(existingCategory); 
     } 
    } 

請注意,怎樣的DbContext妥善處理。

+0

非常感謝 - 它適用於我!的確Include語句接受字符串,而不是lambda:Include(「parentCategory」) –

+0

很高興幫助!接受lambda的包含在從EF 4.1開始的「System.Data.Entity」中。 –