Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 防止在多对多关系中重复插入_Asp.net Mvc_Asp.net Mvc 4_Ef Code First - Fatal编程技术网

Asp.net mvc 防止在多对多关系中重复插入

Asp.net mvc 防止在多对多关系中重复插入,asp.net-mvc,asp.net-mvc-4,ef-code-first,Asp.net Mvc,Asp.net Mvc 4,Ef Code First,我先用MVC4和EF代码5, 我对post和tag实体有多对多关系 public class Post : BaseEntity { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public virtual ICollection<PostTag> Tags { get; set; } }

我先用MVC4和EF代码5, 我对post和tag实体有多对多关系

public class Post : BaseEntity
{

    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public virtual ICollection<PostTag> Tags { get; set; }
}



public class PostTag : BaseEntity
{
    public int TagId { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Post> Posts { get; set; }
}
添加新邮件的我的邮件服务:

public void Add(Post entity)
{
    _posts.Add(entity);
}
我的问题是: 当我添加带有此标记的新post实体时:[A、B、C]

数据库中存在以下标记:[B,H,E]

但是在标签实体中插入新的[B]记录,这是重复的

我必须如何插入只插入[A,C]标签的新帖子

我想成为这个新的职位id是3:

职位:

postid
1
2
3
标签:

PostTagsJ功能:

postid   tagid
1        2
1        3
2        3
2        1
3        1
3        4
3        5

这在网站上不是一个更大的问题吗?如何更新数据库中的信息以创建dup?如果需要的话,你可以发布视图和控制器吗?请再次查看我的问题,这是显而易见的。
tagid   name
1       B
2       H
3       E
4       A
5       C
postid   tagid
1        2
1        3
2        3
2        1
3        1
3        4
3        5