MVC EF - Update navigation property

旧城冷巷雨未停 提交于 2019-12-12 02:22:57

问题


You can see my database diagram below.

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

Controller

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

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

    return View(post);
}

I want to update my Post with navigation properties. Parameter selectedCategories contains a list with ids. (category id).

How can I update PostMapping table?


Edit:

How can I delete an PostMapping object? I tried in this way:

        [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:


You create the PostMapping objects:

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 knows about the PostMappimgs only if you create them, and put them in their proper places.



来源:https://stackoverflow.com/questions/22410441/mvc-ef-update-navigation-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!