简体   繁体   English

实体框架 - 如何在辅助表中的非主键列上连接表?

[英]Entity Framework - How do I join tables on non-primary key columns in secondary tables?

I want to join 2 tables using entity framework. 我想使用实体框架加入2个表。 I want the join to the second table to be on a non-primary key column. 我希望第二个表的连接位于非主键列上。

eg I have a table Foo with fields 例如,我有一个带有字段的表Foo

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. 我想在DbValue领域加入Foo到EF吧。

In hibernate/nhibernate one can do this by adding a column parameter to a many-to-one. 在hibernate / nhibernate中,可以通过向多对一添加列参数来实现。 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. 如果有人知道如何在EF中这样做,请提前感谢。

Well you can't do this as a named relationship (ie the standard way). 那么你不能把它作为命名关系(即标准方式)。

So this means the relationship is NOT part of the model. 所以这意味着这种关系不是模型的一部分。

However you can still do a standard LINQ join though: 但是,您仍然可以执行标准的LINQ连接:

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. 看看我的EF Tips系列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM