简体   繁体   English

实体框架不保存对象属性

[英]Entity framework doesn't save object property

I'm developing online store, using ASP.Net MVC 4 framework. 我正在使用ASP.Net MVC 4框架开发在线商店。

I have an object Category — category of items in store (men's apparel — for instance): 我有一个对象Category-商店中商品的类别(例如,男装):

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>();
    }
}

There is a property parentCategory — which means that this category is included to another one (Men's apparel > Shirts, for instance). 有一个parentCategory属性,这意味着该类别包含在另一个类别中(例如,男士服装>衬衫)。

For editing existing Category I use the following action: 要编辑现有的类别,请执行以下操作:

[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);
}

Everything works fine, except when I want to set parentCategory to null — it sets to null in code, but null value is not being saved to database — previous value still there. 一切正常,除非我想将parentCategory设置为null(在代码中将其设置为null,但null值未保存到数据库中),以前的值仍然存在。

But if I do it like this: 但是,如果我这样做:

[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);
}

ie I set it to null without conditions, it is being saved correctly. 即我将其设置为无条件的null,它已正确保存。

What am I doing wrong? 我究竟做错了什么?

Why there is no Key attribute under Category. 为什么在类别下没有属性。 id ? id If the id is a primary key it must be there. 如果id是主键,则必须在其中。

You should use Include to load relate entities : 您应该使用Include 加载相关实体

   [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);
        }
    }

Please note, how DbContext is properly disposed. 请注意,如何正确处理DbContext。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM