Self-referencing many-to-many relationship EF code first

限于喜欢 提交于 2019-12-05 17:39:02

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)

Don Thomas Boyle

Try this answer: *Note that this is incomplete because i do not see the other table that you are trying to m-2-m with Persons. ( You will only want to use Persons Table and the 2nd Table , NOT table=Friends.

 db.Person
          .Include(c => c.Friends)
          .Where(c => c.Friends.Any(up => up.FriendVlaue == c.FirstName)) //c.from Persons
          .Select(c => new
          {
              PersonID = c.ID,
              PersonName = c.FirstName,
              PersonCount = c.Person.Count()
          })

         {

From This answer

You should include Friends in the results. You can do this by adding Include("Friends")at Server Side API.

[HttpGet]
public IQueryable<Person> Persons()
{
   return _contextProvider.Persons.Include("Friends");
}

If you don't want to return always the Friendsreference, you can create another method in the API such as PersonsWithFriends as suggested in here (Specialized query actions).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!