MVC EF - Update navigation property

2019-08-28 05:54发布

问题:

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.