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

时间:2021-04-01 18:54:20

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.

我正在使用Entity Framework 4 CTP 5 POCO构建一个个人博客应用程序,其中一个帖子可以有很多标签,一个标签可以在很多帖子中。我的问题是,是建立一个多对多模型,还是有一个查找表。

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:

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

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

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:

问题是我不确定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()
        };

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

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

2 个解决方案

#1


0  

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

我认为您的模型应该按原样进行轻微调整:向PostTagLookup实体添加ID列/字段。

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?

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

#2


0  

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:

在过去的几天里我一直在努力,并且决定按照预期使用Lookup表,并且在Lookup表中,还引用了Post和Tag模型,如下所示:

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.

可能不是最好的方式,但它的工作:)谢谢大家的期待。

#1


0  

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

我认为您的模型应该按原样进行轻微调整:向PostTagLookup实体添加ID列/字段。

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?

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

#2


0  

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:

在过去的几天里我一直在努力,并且决定按照预期使用Lookup表,并且在Lookup表中,还引用了Post和Tag模型,如下所示:

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.

可能不是最好的方式,但它的工作:)谢谢大家的期待。