实体框架 4 CTP 5 POCO - 多对多或查找表?

发布于 2024-10-09 21:09:16 字数 2077 浏览 0 评论 0原文

我正在仅使用 Entity Framework 4 CTP 5 POCO 构建一个个人博客应用程序,其中 Post 可以有许多 Tags 并且 Tag 可以在许多帖子中。我的问题是是否建立一个多对多模型,或者有一个查找表。

起初我尝试使用多对多,但我不知道如何插入,每次发布新帖子(选择了许多标签)时,我不知道该怎么做才能使标签应该与帖子相关联(如果标签名称已经存在,则不会插入新标签。)

然后我尝试构建一个查找表,如下所示:

Post

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<PostTagLookup> PostTagLookups { get; set; }
}

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 string FriendlyName { get; set; }
    public virtual ICollection<PostTagLookup> PostTagLookups { get; set; }
}

PostTagsLookup

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

问题是我不确定 EF 将如何处理查找表(当我选择标签时,如何获取帖子的标签或帖子的集合)。使用下面的代码,我收到一条错误消息,指出 PostTagLookup 没有 Id 键:

var getPosts = _post.GetLatestPosts(3).ToList();

var posts = from post in getPosts
        select new PostModel
        {
            Id = post.Id,
            Title = post.Title,
            Content = post.Content,
            FriendlyUrl = post.FriendlyUrl,
            PostedDate = post.PostedDate,
            IsActive = post.IsActive,
            NumberOfComments = post.Comments.Count(),
            PostTags = post.PostTagLookups.Where(p => p.PostId == post.Id).ToList()
        };

关于如何完成此任务的任何建议?非常感谢!

I'm building a personal blog app with Entity Framework 4 CTP 5 POCO only, where a Post can have many Tags and a Tag can be in many Posts. My question is whether to build a many-to-many model, or to have a lookup table.

At first I was trying to use many-to-many, but I don't know how to do insertion, each time a new post is posted (with many tags selected), I'm not sure what to do so that the tags should be associated with the post (and wouldn't insert a new tag if the tag name already exists.)

I then try to build a Lookup table like so:

Post

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<PostTagLookup> PostTagLookups { get; set; }
}

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 string FriendlyName { get; set; }
    public virtual ICollection<PostTagLookup> PostTagLookups { get; set; }
}

PostTagsLookup

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

The problem is I'm not sure how EF are going to handle lookup table (how will I get the Tags of a Post, or a collection of the Posts when I select a Tag). And with the code below, I got an error saying PostTagLookup doesn't have an Id key:

var getPosts = _post.GetLatestPosts(3).ToList();

var posts = from post in getPosts
        select new PostModel
        {
            Id = post.Id,
            Title = post.Title,
            Content = post.Content,
            FriendlyUrl = post.FriendlyUrl,
            PostedDate = post.PostedDate,
            IsActive = post.IsActive,
            NumberOfComments = post.Comments.Count(),
            PostTags = post.PostTagLookups.Where(p => p.PostId == post.Id).ToList()
        };

Any suggestion on how to accomplish this task? Thank you very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

⊕婉儿 2024-10-16 21:09:16

我认为您的模型应该按原样工作,稍作调整:将 ID 列/字段添加到 PostTagLookup 实体。

public class PostTagLookup
{
    [Key]
    [DatabaseGenerated(DatabaseGenerationOption.Identity)]
    public int PostTagLookupId { get; set; }

    //etc.
}

但是,我不确定为什么您不希望 EF 自己处理底层的多对多。例如,当您有一个新的 Post 对象时,您所要做的就是在对上下文调用 SaveChanges() 之前将任何关联的 Tags 添加到实例化的 Post 的 Tags 集合中。这对你不起作用吗?

I think your model should work as-is with a slight tweak: add an ID column/field to the PostTagLookup entity.

public class PostTagLookup
{
    [Key]
    [DatabaseGenerated(DatabaseGenerationOption.Identity)]
    public int PostTagLookupId { get; set; }

    //etc.
}

However, I'm not sure why you wouldn't want EF to handle the underlying many-to-many on it's own. When you have a new Post object, for example, all you have to do is add any associated Tags to the instantiated Post's Tags collection before calling SaveChanges() on your context. Did this not work for you?

月亮邮递员 2024-10-16 21:09:16

在过去的几天里,我一直在努力解决这个问题,并决定按预期使用查找表,并且在查找表中,还引用了帖子和标签模型:

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've struggled with it for the past couple days and decided to go with the Lookup table as intended, and in the Lookup table, also have references to Post and Tag model as such:

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

Might not be the best way but it's working :) Thanks all for looking.

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