Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

c# - Entity Framework - How do I join tables on non-primary key columns in secondary tables?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...