MVC EF - 更新导航属性

时间:2014-03-14 16:16:12

标签: c# asp.net-mvc entity-framework updatemodel

您可以在下面看到我的数据库图表。

enter image description here

Post.cs

public partial class Post
{
    public Post()
    {
        this.PostImages = new HashSet<PostImage>();
        this.PostMappings = new HashSet<PostMapping>();
    }

    public int ID { get; set; }
    public string Title { get; set; }
    public string TitleMenu { get; set; }
    public string Preview { get; set; }
    public string Content { get; set; }
    public Nullable<bool> Visible { get; set; }
    public Nullable<int> Display { get; set; }
    public Nullable<System.DateTime> DateAdded { get; set; }
    public Nullable<System.DateTime> DateHide { get; set; }
    public Nullable<int> PozitionMenu { get; set; }
    public string Username { get; set; }

    public virtual ICollection<PostImage> PostImages { get; set; }
    public virtual ICollection<PostMapping> PostMappings { get; set; }
}

Category.cs

public partial class Category
{
    public Category()
    {
        this.CourseMappings = new HashSet<CourseMapping>();
        this.PostMappings = new HashSet<PostMapping>();
    }

    public int ID { get; set; }
    public string Name { get; set; }
    public Nullable<bool> Visible { get; set; }
    public string Color { get; set; }
    public Nullable<int> Display { get; set; }
    public Nullable<bool> IsBlog { get; set; }

    public virtual ICollection<CourseMapping> CourseMappings { get; set; }
    public virtual ICollection<PostMapping> PostMappings { get; set; }
}

PostMapping.cs

public partial class PostMapping
{
    public System.Guid ID { get; set; }
    public Nullable<int> PostID { get; set; }
    public Nullable<int> CategoryID { get; set; }

    public virtual Category Category { get; set; }
    public virtual Post Post { get; set; }
}

控制器

public ActionResult Edit(Post post, string[] selectedCategories)
{
    if (ModelState.IsValid)
    {          
        db.Entry(post).State = EntityState.Modified;

        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(post);
}

我想用导航属性更新我的帖子。 参数 selectedCategories 包含带有ID的列表。 (类别ID)。

如何更新 PostMapping 表?


编辑:

如何删除PostMapping对象? 我试着用这种方式:

        [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(Post post, int[] selectedCategories)
    {



        if (ModelState.IsValid)
        {
            List<PostMapping> list_already = db.PostMappings.Where(p => p.PostID == post.ID).ToList();


            post.PostMappings = list_already;


            foreach (PostMapping pm in list_already)
            {
                int categoryId = pm.CategoryID.Value;
                if (selectedCategories != null)
                {
                    if (selectedCategories.Contains(categoryId))
                    {
                        selectedCategories = selectedCategories.Where(val => val != categoryId).ToArray();
                    }
                }
                else
                {

                    post.PostMappings.Remove(pm);
                    Category category = db.Categories.Where(c => c.ID == categoryId).SingleOrDefault();
                    category.PostMappings.Remove(pm);
                }
            }

            foreach (var id in selectedCategories)
            {
                Category category = db.Categories.Where(c => c.ID == id).SingleOrDefault();
                PostMapping postMap = new PostMapping();
                postMap.Category = category;
                postMap.Post = post;
                postMap.ID = Guid.NewGuid();

                post.PostMappings.Add(postMap);


                category.PostMappings.Add(postMap);

            }

            db.Entry(post).State = EntityState.Modified;


            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(post);
    }

1 个答案:

答案 0 :(得分:1)

您创建PostMapping对象:

foreach (string categoryName in selectedCategories)
{
    Category category = LoadCategoryForName(categoryName);
    PostMapping postMap = new PostMapping();
    postMap.Category = category;
    postMap.Post = post;
    // Add the maps to cat and post
    post.PostMaps.Add(postMap);
    category.PostMaps.Add(postMap);
}

// update the db.

EF只有在你创建PostMappimgs时才知道它们,并将它们放在适当的位置。