自我引用多对多关系EF代码 [英] Self-referencing many-to-many relationship EF code first
问题描述
假设我有以下类:
public class Person
{
public int Id {get;组; }
public string Firstname {get;组; }
public string Lastname {get;组; }
public virtual List< Person>新新旗新新新新新新新新新新新新新新新新新新新新新组;
}
使用以下EF Fluent API:
modelBuilder.Entity< Person>()
.HasMany(m => m.Friends)
.WithMany()
.Map(m => m.ToTable(Friends));
数据库生成成功。
问题是当我执行一个que
ry with Breeze(客户端)没有数据为朋友
属性。
var query = entityQuery.from ('Person')
.where('id','==',123)
.expand(好友);
当执行查询时,我得到所有People实体的所有数据,除了Friends属性总是一个空数组。当我检查Json答案时,我看到数据也被传输。甚至是Friend属性的数据。但是他们没有链接到好友属性本身。
我的问题:
谢谢。
你必须声明一个外键人。微风要求FK正确地解决关联。
编辑:
意识到你在询问多对多的关系。 (是的,我应该看过帖子标题...)
微风不支持多对多关联。
但是,您可以拥有两个一对多的关系,作为一对多的关系。 (即多对一)在这种情况下,您将需要如前所述定义链接表/实体和外键。 (请参阅 http://www.breezejs.com/documentation/navigation-properties )
I work with ASP.NET MVC With Durandal/Breeze templates.
Let's say I have the following class:
public class Person
{
public int Id { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public virtual List<Person> Friends { get; set; }
}
With the following EF Fluent API:
modelBuilder.Entity<Person>()
.HasMany(m => m.Friends)
.WithMany()
.Map(m => m.ToTable("Friends"));
The database is generated successfully.
The problem is when I perform a que
ry with Breeze (client side) I have no data for the Friends
property.
var query = entityQuery.from('Person')
.where('id', '==', 123)
.expand("Friends");
When the query is executed I get as result the requested People entity with all the data except the Friends property is always an empty array. When I check the Json answer I see that also the data are transmitted. Even data for the Friends property. However they are not linked to the Friends property itself.
My question: what do I have to do to have my Friends property filled with values?
Thanks.
You must declare a foreign key in Person. Breeze requires the FK to correctly resolve associations.
Edit:
I just realized you are asking about a many-to-many relationship. (yeah, I should have read the post title...) Breeze does not support many-to-many associations. However, you could have two one-to-many relationships to work as a many-to-many. (i.e. many-to-one-to-many) In this case, you will need to define the linking table/entity and the foreign key as mentioned earlier. (see http://www.breezejs.com/documentation/navigation-properties)
这篇关于自我引用多对多关系EF代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!