Many to one relationship, unwanted JOIN

Krystian

I have many to one relationship defined within my model: Project.CreatedBy = ApplicationUser.Id:

class Project{
[Required]
public ApplicationUser CreatedBy {get; set;}
}

With mapping:

modelBuilder.Entity<Project>().HasOptional(i => i.CreatedBy).WithOptionalDependent().WillCascadeOnDelete(false);

Unfortunatelly, when I try to retrieve all users from DbContext by:

var users = context.Users;

Generated SQL looks like:

SELECT 
[...]
FROM     [dbo].[AspNetUsers] AS [Extent1]
LEFT OUTER JOIN [dbo].[Projects] AS [Extent4] ON ([Extent4].[CreatedBy_Id] IS NOT NULL) AND ([Extent1].[Id] = [Extent4].[CreatedBy_Id])

So when certain user have created 10 projects, the user entity is multiplied 10 times. It makes me impossible to look for that user by his username:

context.Users.Single(u => u.Username == "test")

because it would give me Sequence contains more than one element exception.

Do you have any idea how to avoid that extra join?

I suspect it has something to do with modelBuilder declaration. I've been googling about this, but never found any solution.

Any materials about modelBuilder and defining relationships with it would be really appreciated too.

Gert Arnold

The mapping...

modelBuilder.Entity<Project>()
            .HasOptional(i => i.CreatedBy)
            .WithOptionalDependent()

...expresses this as a 1:1 association, not the 1:n association you intend it to be.

So change the mapping into one-to-many:

modelBuilder.Entity<Project>()
            .HasOptional(p => p.CreatedBy)
            .WithMany()
            .HasForeignKey(p => p.CreatedBy_Id);

I'm not sure why EF joins in the Project when you query User because the association is optional), but a proper 1:n mapping will stop this.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sqlalchemy one to many relationship join?

From Dev

Hibernate one to many relationship with join table with addition columns in join table

From Dev

SQL join one to many relationship - count number of votes per image?

From Dev

Oracle SQL Developer - JOIN on 2 queries with a one-to-many relationship

From Dev

Linq to Sql One to many relationship string.join

From Dev

mysql join one to many relationship and print in different rows

From Dev

result repetition in SQL inner join with one to many relationship

From Dev

How to join a one-to-many relationship in Entity Framework?

From Dev

SQL Query with Aggregate function on Left Join of One-to-Many Relationship

From Dev

One To Many Relationship In Firebase

From Dev

Doctrine One to Many relationship

From Dev

Seeding one to many relationship

From Dev

Themeing a one to many relationship

From Dev

One to many relationship table

From Dev

Paginate a One to Many Relationship

From Dev

Laravel: one to many relationship

From Dev

Accessing a one to many relationship

From Dev

Displaying one to many relationship

From Dev

Implementing one to many relationship

From Dev

MagicalRecord: one to many relationship

From Dev

Paginate a One to Many Relationship

From Dev

Seeding one to many relationship

From Dev

One to many relationship with NSfetchedresultscontroller

From Dev

one to many relationship in laravel

From Dev

EntityFramework - One to many relationship

From Dev

One to many relationship in springboot

From Dev

One to many + one relationship in SQLAlchemy?

From Dev

Exclusive join on has many relationship

From Dev

one to many vs many to many relationship

Related Related

HotTag

Archive