First EF Code - WithMany ()

c# ef-code-first entity-framework

Question

I just dropped by the class.ManyNavigationPropertyConfiguration<TEntity, TTarget> , and I discovered a method called "" inside of that class.WithMany() having two overloads.

Initial overload:WithMany()

Configures the relationship to be many:many without a navigation property on the other side of the relationship.

Another overload:WithMany(Expression<Func<TTarget, ICollection<TEntity>>>)

Configures the relationship to be many:many with a navigation property on the other side of the relationship.

The reason you would set a relationship to be many:many without a navigation property (the first overload) is the subject of my next inquiry. I don't envision any circumstances in which that would be useful. Any suggestions?

1
14
5/8/2011 11:56:53 AM

Accepted Answer

This design might serve as an example:

public class User
{
    public int UserId { get; set; }
    public string Name { get; set; }
    public ICollection<Role> Roles { get; set; }
}

public class Role
{
    public int RoleId { get; set; }
    public string Description { get; set; }
}

adding a navigation attribute with the code If retrieving all users who are in a certain job is never something you're interested in,

public ICollection<User> Users { get; set; }

... to theRole It would be superfluous to have class.

You must however emphasize that there is a many-to-many interaction betweenUser and Role there is...

modelBuilder.Entity<User>()
            .HasMany(u => u.Roles)
            .WithMany();

... since the default convention mappings would result in this mapping having a one-to-many connection, which is the incorrect relationship:

modelBuilder.Entity<User>()
            .HasMany(u => u.Roles)
            .WithOptional();
37
5/8/2011 12:18:02 PM

Popular Answer

Keep in mind that on the other side of the goal is the option for a navigation property.

Even if this particular instance may not be the best illustration of my idea, let's look at it nevertheless. You may have two tables if you wish to keep track of math exams and reuse questions.Tests and Questions ) that are linked in a many-to-many way; each exam comprises a number of questions, and each question may appear on a number of tests. Even if you are aware that a question may occur on more than one exam, you may never need to get a collection of tests that include that particular question.
So, you use the.WithMany() overflow when declaring this, resulting in the acquisition of a navigational property to access test questions (theTest.Questions() (although there is no navigational property going the opposite direction. (theQuestion.Tests() ). However, because both tests and questions might include many of the other, you still require a many-to-many connection.
I agree that this configuration may not make sense in this particular instance, but there are instances when it does, and in other.WithMany() By using overload, you may avoid creating attributes (and corresponding lambda expressions) that you'll never use.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow