Self-regulation of relationships to many relationships EF to many

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 was generated successfully.

The problem is that I am doing 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 request is executed, I get the requested People object with all the data except the Friends property, which is always an empty array. When I check the Json response, I see that the data is also being transmitted. Even the data for the Friends property. However, they are not affiliated with the Friends property.

My question is: what do I need to do so that my friends property is populated with values?

Thanks.

+4
source share
3 answers

You must declare the foreign key in the Personal. The breeze requires the FK to correctly allow associations.

Edit:

I just realized that you are asking about many-to-many relationships. (yes, I should have read the title of the post ...) The breeze does not support many-to-many associations. However, you may have two one-to-many relationships to work as many-to-many relationships. (i.e. many-to-one-to-many). In this case, you will need to define the table / binding object and foreign key, as mentioned earlier. (see http://www.breezejs.com/documentation/navigation-properties )

+1
source

Try this answer: * Note that this is incomplete, because I do not see another table that you are trying m-2-m with people. (You only need to use the persons table and the second table, not the 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

+1
source

You must include friends in the results. You can do this by adding Include("Friends") to the server side API.

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

If you do not want to always return the Friends link, you can create another method in the API, for example PersonsWithFriends , as suggested in here (Special request actions).

0
source

Source: https://habr.com/ru/post/1498005/


All Articles