实体框架4多对多插入?
我不太熟悉使用 Entity Framework 4 POCO 的多对多插入过程。我有一个包含 3 个表的博客:帖子、评论和标签。一个帖子可以有多个标签,一个标签可以包含在多个帖子中。以下是 Post 和 Tag 模型:
public class Tag
{
public int Id { get; set; }
[Required]
[StringLength(25, ErrorMessage = "Tag name can't exceed 25 characters.")]
public string Name { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
public class Post
{
public int Id { get; set; }
[Required]
[StringLength(512, ErrorMessage = "Title can't exceed 512 characters")]
public string Title { get; set; }
[Required]
[AllowHtml]
public string Content { get; set; }
public string FriendlyUrl { get; set; }
public DateTime PostedDate { get; set; }
public bool IsActive { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
现在,当我添加新帖子时,我不确定正确的做法是什么。我想我将有一个文本框,我可以在其中为该帖子选择多个标签(这部分已经完成),在我的控制器中,我将检查该标签是否已经存在,如果不存在,那么我将插入新标签。但我什至不确定根据我为 EF 创建的模型,他们会创建一个 PostsTags 表,还是只创建一个 Tags 和一个 Posts 表以及两者之间的链接?
如何插入新的帖子并为该帖子设置标签?它只是 newPost.Tags = Tags
(其中 Tags 是被选择的标签,我是否需要检查它们是否已经存在?),然后是类似 _post.Add 的内容(新帖子);
?
谢谢。
I'm not very familiar with the many-to-many insertion process using Entity Framework 4, POCO. I have a blog with 3 tables: Post, Comment, and Tag. A Post can have many Tags and a Tag can be in many Posts. Here are the Post and Tag models:
public class Tag
{
public int Id { get; set; }
[Required]
[StringLength(25, ErrorMessage = "Tag name can't exceed 25 characters.")]
public string Name { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
public class Post
{
public int Id { get; set; }
[Required]
[StringLength(512, ErrorMessage = "Title can't exceed 512 characters")]
public string Title { get; set; }
[Required]
[AllowHtml]
public string Content { get; set; }
public string FriendlyUrl { get; set; }
public DateTime PostedDate { get; set; }
public bool IsActive { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
Now when I'm adding a new post, I'm not sure what would be the right way to do. I'm thinking that I'll have a textbox where I can select multiple tags for that post (this part is already done), in my controller, I will check to see if the tag is already exists or not, if not, then I will insert the new tag. But I'm not even sure based on the models that I've created for EF, will they create a PostsTags table, or they are creating just a Tags and a Posts table and links between the two?
How would I insert the new Post and set the tags to that post? Is it just newPost.Tags = Tags
(where Tags are the one that got selected, do I even need to check to see if they already exists?), and then something like _post.Add(newPost);
?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这就是你想要的。
新年快乐编码))
I think that this is what you want.
Happy New-Year coding))
我建议不要在数据库中维护多对多关系。
您应该考虑创建一个名为
PostTag
或PostTagLookup
之类的查找表。这将由两列组成 -
PostId
和TagId
。实体框架可以很好地处理查找表,正如 @Moyo 在 他的回答:
I would advise against maintaining many-to-many relationships in your database.
You should consider creating a lookup table called something like
PostTag
orPostTagLookup
.This would consist of two columns -
PostId
andTagId
.Entity framework handles lookup tables wonderfully, as mentioned by @Moyo in his answer:
如果没有查找表的 ID,我似乎无法让它工作,这就是我为使其工作而所做的。可能不是最好、最有效的方法,但它确实有效:
I can't seems to get it working without the Id for the Lookup table, this is what I've done to make it work. Might not be the best and most efficient way but it's working: