Entity framework 不保存对象 属性
Entity framework doesn't save object property
我正在开发在线商店,使用 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 — 这意味着该类别包含在另一个类别中(例如男装 > 衬衫)。
要编辑现有类别,我使用以下操作:
[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,但 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,保存正确
我做错了什么?
为什么Category.id下没有Key属性?如果 id 是主键,它必须存在。
你应该使用 Include 到 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 是如何正确处理的。
我正在开发在线商店,使用 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 — 这意味着该类别包含在另一个类别中(例如男装 > 衬衫)。
要编辑现有类别,我使用以下操作:
[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,但 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,保存正确
我做错了什么?
为什么Category.id下没有Key属性?如果 id 是主键,它必须存在。
你应该使用 Include 到 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 是如何正确处理的。