实体框架 - 如何在辅助表中的非主键列上连接表? [英] Entity Framework - How do I join tables on non-primary key columns in secondary tables?

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

问题描述

我想使用实体框架加入2个表。我希望第二个表的连接位于非主键列上。



例如我有一个表Foo与字段

  Foo.Id(PK)
Foo.DbValue

和表吧

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

我想在DbValue字段的EF中加入Foo to Bar。



在hibernate / nhibernate中,可以通过将列参数添加到多对一来实现。大致如下

 < class name =Footable =Foo> 
< id name = Idcolumn =Id/>
< many-to-one name =Barclass =Barcolumn =** DbValue **/>
< class>

如果有人知道如何在EF中执行此操作,请提前感谢。

解决方案

你不能这样做作为一个命名关系(即标准方式)。



所以这意味着关系不是模型的一部分。



但是,您仍然可以通过以下方式执行标准LINQ连接:


$ b $ f code $ c从ctx.Foo
中的f加载b在f.DbValue上的ctx.Bar中等于b.DbValue
选择新的{f,b}

希望这有帮助



查看我的<一个href =http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx =nofollow noreferrer> EF Tips系列


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.

解决方案

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天全站免登陆