实体框架4多对多插入?

发布于 2024-10-10 07:57:59 字数 1361 浏览 3 评论 0原文

我不太熟悉使用 Entity Framework 4 POCO 的多对多插入过程。我有一个包含 3 个表的博客:帖子、评论和标签。一个帖子可以有多个标签,一个标签可以包含在多个帖子中。以下是 PostTag 模型:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

高跟鞋的旋律 2024-10-17 07:57:59

我认为这就是你想要的。

var tag = context.Tags.FirstOrDefault(x=>x.Name == name);
if (tag == null)
    context.Tags.AddObject(tag = new Tag { Name = name });
if (!post.Tags.Contains(tag))
    post.Tags.Add(tag);

新年快乐编码))

I think that this is what you want.

var tag = context.Tags.FirstOrDefault(x=>x.Name == name);
if (tag == null)
    context.Tags.AddObject(tag = new Tag { Name = name });
if (!post.Tags.Contains(tag))
    post.Tags.Add(tag);

Happy New-Year coding))

煞人兵器 2024-10-17 07:57:59

我建议不要在数据库中维护多对多关系。

您应该考虑创建一个名为 PostTagPostTagLookup 之类的查找表。

这将由两列组成 - PostIdTagId

实体框架可以很好地处理查找表,正如 @Moyo 在 他的回答

实体框架会将映射表转换为两侧实体的集合,并且表本身将基本上消失。

I would advise against maintaining many-to-many relationships in your database.

You should consider creating a lookup table called something like PostTag or PostTagLookup.

This would consist of two columns - PostId and TagId.

Entity framework handles lookup tables wonderfully, as mentioned by @Moyo in his answer:

Entity Framework will convert the mapping table into a collection of entities on both sides and the table itself will essentially disappear.

想你的星星会说话 2024-10-17 07:57:59

如果没有查找表的 ID,我似乎无法让它工作,这就是我为使其工作而所做的。可能不是最好、最有效的方法,但它确实有效:

public class PostTagLookup
{
    public int Id { get; set; }
    public int PostId { get; set; }
    public int TagId { get; set; }

    public virtual Post Post { get; set; }
    public virtual Tag Tag { get; set; }
}

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:

public class PostTagLookup
{
    public int Id { get; set; }
    public int PostId { get; set; }
    public int TagId { get; set; }

    public virtual Post Post { get; set; }
    public virtual Tag Tag { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文