实体框架的工作:多对多的关系表 [英] Entity frame work: many to many relationship tables

查看:142
本文介绍了实体框架的工作:多对多的关系表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个新闻机构,我得到基于他们NewSID的消息。现在我定义了一个新的实体,一个组,我想根据自己的组ID的消息。我定义了一个新闻组表ASLO到涉及该表在一起。

在新闻模型我有:

  public虚拟的ICollection< GroupNews> RelatedGroupID {搞定;组; }

所以,我认为我所定义的GroupNews表值,我可以在NewsService使用它。

现在让我们看一下NewsService:

 防爆pression<&Func键LT;新闻,布尔>>约束= NULL;    如果(USER_ID大于0&放大器;&放大器; PROJECT_ID大于0)
    {
        约束= E => (e.CreatorID == USER_ID&放大器;&安培; e.RelatedProjectTags.Any(P => p.ProjectID == PROJECT_ID));
    }
    否则如果(USER_ID大于0)
    {
        约束= E => (e.CreatorID == USER_ID);
    }
    否则如果(PROJECT_ID大于0)
    {
        约束= E => (e.RelatedProjectTags.Any(P => p.ProjectID == PROJECT_ID));
    }    其他
    {
        约束= NULL;
    }    IEnumerable的<新闻> result_list = NULL;    如果(约束!= NULL)
    {
        result_list = newsRepository.GetMany(约束).OrderByDescending(E => e.CreatedOn).Skip(偏移);
    }
    其他
    {
        result_list = newsRepository.GetAll()OrderByDescending(E => e.CreatedOn)。.Skip(补偿);
    }    如果(计数大于0)
    {
        result_list = result_list.Take(计数);
    }    返回result_list.ToList<新闻>();
}

}

我为了定义基于组ID约束这一行添加到它。

 否则如果(的groupId大于0)
    {
        约束= E => (e.RelatedGroupID.Any(N => n.GroupID ==的groupId));
    }

似乎错了,给我这个错误:


  

{无效的对象名称dbo.GroupNewsNews'。}



解决方案

1.You不需要GroupNewsID在GroupNews表。您需要删除
    此列,并创建以groupId和NewSID的复杂的组合键。在
    新闻实体您需要定义属性:

  public虚拟的ICollection<组>组
    {
        得到;
        组;
    }

在此实体需要初始化属性的默认构造函数(需要延迟加载):

 组=新的List<组>();

类似的变化对集团实体。

2.In您需要定义GroupMap.cs

  this.HasMany(T => t.News)
    .WithMany(T => t.Groups)
    .MAP(M =>
        {
            m.ToTable(GroupNews);
            m.MapLeftKey(组ID);
            m.MapRightKey(NewSID的);
        });

3.Write测试NewsRepository和的Grou prepository。

I have a news entity and I get the news based on their NewsID. Now I defined a new entity , a Group and I want to get the news based on their Group ID. I defined a Group news Table Aslo to relate this to table together.

in news model I have :

public virtual ICollection<GroupNews> RelatedGroupID { get; set; }

So I Assumed that I defined the GroupNews table values and I can use it in NewsService.

Now lets look at NewsService :

    Expression<Func<News, bool>> constraint = null;

    if (user_id > 0 && project_id > 0)
    {
        constraint = e => (e.CreatorID == user_id && e.RelatedProjectTags.Any(p => p.ProjectID == project_id));
    }
    else if (user_id > 0)
    {
        constraint = e => (e.CreatorID == user_id);
    }
    else if (project_id > 0)
    {
        constraint = e => (e.RelatedProjectTags.Any(p => p.ProjectID == project_id));
    }

    else
    {
        constraint = null;
    }

    IEnumerable<News> result_list = null;

    if (constraint != null)
    {
        result_list = newsRepository.GetMany(constraint).OrderByDescending(e => e.CreatedOn).Skip(offset);
    }
    else
    {
        result_list = newsRepository.GetAll().OrderByDescending(e => e.CreatedOn).Skip(offset);
    }

    if (count > 0)
    {
        result_list = result_list.Take(count);
    }

    return result_list.ToList<News>();
}

}

I add this line to it in order to define a constraint based on GroupID.

    else if (groupId > 0)
    {
        constraint = e => (e.RelatedGroupID.Any(n => n.GroupID == groupId));
    }

it seems wrong and gives me this error :

{"Invalid object name 'dbo.GroupNewsNews'."}

解决方案

1.You does not need GroupNewsID in GroupNews table. You need to drop this column and create complex key by GroupID and NewsID. In the News entity you need to define property:

    public virtual ICollection<Group> Groups 
    { 
        get; 
        set; 
    }

In the default constructor for this entity you need to initialize property(need for lazy load):

Groups = new List<Group>();

Similar changes for Group entity.

2.In the GroupMap.cs you need to define

this.HasMany(t => t.News)
    .WithMany(t => t.Groups)
    .Map(m =>
        {
            m.ToTable("GroupNews");
            m.MapLeftKey("GroupID");
            m.MapRightKey("NewsID");
        });

3.Write tests for NewsRepository and GroupRepository.

这篇关于实体框架的工作:多对多的关系表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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