MVC EF - 更新导航属性 [英] MVC EF - Update navigation property

查看:239
本文介绍了MVC EF - 更新导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





Post.cs

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

public int ID {get;组; }
public string标题{get;组; }
public string TitleMenu {get;组; }
public string Preview {get;组; }
public string Content {get;组; }
public Nullable< bool>可见{get;组; }
public Nullable< int>显示{get;组; }
public Nullable< System.DateTime> DateAdded {get;组; }
public Nullable< System.DateTime> DateHide {get;组; }
public Nullable< int> PozitionMenu {get;组; }
public string Username {get;组; }

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

Category.cs

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

public int ID {get;组; }
public string Name {get;组; }
public Nullable< bool>可见{get;组; }
public string Color {get;组; }
public Nullable< int>显示{get;组; }
public Nullable< bool> IsBlog {get;组; }

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

PostMapping.cs

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

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

控制器

  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 包含带有ids的列表。 (类别ID)



如何更新PostMapping 表?






编辑:



如何删除PostMapping对象?
我以这种方式尝试:

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



if(ModelState.IsValid)
{
列表< 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 = db.Categories.Where(c => c.ID == categoryId).SingleOrDefault();
category.PostMappings.Remove(pm);
}
}

foreach(selectedCategories中的var id)
{
类别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);
}


解决方案

您创建PostMapping对象:

  foreach(selectedCategories中的string categoryName)
{
类别category = LoadCategoryForName(categoryName);
PostMapping postMap = new PostMapping();
postMap.Category = category;
postMap.Post = post;
//将地图添加到cat和post
post.PostMaps.Add(postMap);
category.PostMaps.Add(postMap);
}

//更新db。

只有在创建PostMappimgs并将其放置在正确的位置时,才知道PostMappimgs。 p>

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

解决方案

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.

这篇关于MVC EF - 更新导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆