实体框架4 CTP 5 POCO-多对多还是查找表? [英] Entity Framework 4 CTP 5 POCO - Many-to-many or Lookup table?

查看:95
本文介绍了实体框架4 CTP 5 POCO-多对多还是查找表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仅使用Entity Framework 4 CTP 5 POCO构建个人博客应用,其中帖子可以具有许多标签标签可以出现在许多帖子中.我的问题是要建立多对多模型还是要有查找表.

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:

发布

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

标记

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密钥:

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!

推荐答案

在过去的几天里,我一直在努力解决它,并决定按预期使用Lookup表,并且在Lookup表中也有对Post的引用.以及Tag模型:

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.

这篇关于实体框架4 CTP 5 POCO-多对多还是查找表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆