实体框架 - 我如何加入对非主键列的表在次表? [英] Entity Framework - How do I join tables on non-primary key columns in secondary tables?

查看:107
本文介绍了实体框架 - 我如何加入对非主键列的表在次表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加入使用实体框架2表。我想联接到第二表是对非主键列。



例如。我有一个字段的表美孚

  Foo.Id(PK)
Foo.DbValue

和表列

  Bar.Id(PK)
Bar.DbValue
Bar.Description

和我想加入美孚在EF到栏上的DbValue领域。



在休眠/ NHibernate的人们可以通过添加一列参数来做到这一点多到的。大致是这样的。



 <类名=富表=富> 
< ID名称= ID列=ID/>
<多到一个名称=酒吧级=栏栏=** DbValue **/>
< /类>

在此先感谢,如果有人知道如何做到这一点的EF
选中<。 / p>

解决方案

那么你不能做到这一点作为一个已命名的关系(即标准方式)。



因此,这意味着关系不是模型的一部分​​



但是你仍然可以做一个标准的LINQ虽然加入:

$在ctx.Foo
b
$ b

 从步骤f JOIN b在ctx.Bar上f.DbValue等于b.DbValue 
选择新{ F,b}

希望这有助于



看看我的 EF系列的提示


I want to join 2 tables using entity framework. I want the join to the second table to be on a non-primary key column.

e.g. I have a table Foo with fields

Foo.Id (PK)
Foo.DbValue

and table Bar

Bar.Id (PK)
Bar.DbValue
Bar.Description

And I want to join Foo to Bar in EF on the DbValue field.

In hibernate/nhibernate one can do this by adding a column parameter to a many-to-one. roughly like this

<class name="Foo" table="Foo>
  <id name="Id" column="Id" />
  <many-to-one name="Bar" class="Bar" column="**DbValue**" />
</class>

Thanks in advance if anyone knows how to do this in EF. Mark

解决方案

Well you can't do this as a named relationship (i.e. the standard way).

So this means the relationship is NOT part of the model.

However you can still do a standard LINQ join though:

from f in ctx.Foo
join b in ctx.Bar on f.DbValue equals b.DbValue
select new {f,b} 

Hope this helps

Check out my EF Tips series.

这篇关于实体框架 - 我如何加入对非主键列的表在次表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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