EF 6 Fluent API with One to Many AND Many To Many and conflicting multiplicities

c# ef-fluent-api entity-framework entity-framework-6

Question

I haven't been able to find an answer to this specific problem, perhaps to my newness with Entity Framework

I have two Entities: User and Facility.

The Facility table requires an Owner (user), but a Facility can also have many "FacilityUsers" assigned to a Facility.

public class User
{
    ....
    public virtual ICollection<Facility> Facilities { get; set; }
}

public Facility Facility
{
    public Guid OwnerId { get; set; }
    ...
    public virtual User Owner { get; set; }
    public virtual ICollection<User> FacilityUsers { get; set; }
}

Here is my ModelBuilder

////Many To Many: Users To Facilities
modelBuilder.Entity<User>()
    .HasMany(i => i.Facilities)
    .WithMany(u => u.FacilityUsers)
    .Map(m =>
    {
        m.MapLeftKey("UserId");
        m.MapRightKey("FacilityId");
        m.ToTable("UserFacility");
    });        

//One to Many: Facility To Owner
modelBuilder.Entity<Facility>()
    .HasRequired<User>(s => s.Owner)
    .WithMany(s => s.Facilities)
    .HasForeignKey(k => k.OwnerId);

The problem is, when I have the map Facility to Owner, I get the following error: The navigation property 'FacilityUsers' declared on type 'MEH.Web.Models.Entities.Facility' has been configured with conflicting multiplicities.

However, when I exclude the Facility to Owner mapping, it works.

//One to Many: Facility To Owner
//modelBuilder.Entity<Facility>()
//    .HasRequired<User>(s => s.Owner)
//    .WithMany(s => s.Facilities)
//    .HasForeignKey(k => k.OwnerId);

The problem is, the Facilities table then has a Owner_UserId field that is null, but the OwnerId is correctly filled in. My OCD won't let me move on until I have resolved the "Owner_UserId" problem.

Thank you, D

1
0
5/14/2017 3:43:24 PM

Accepted Answer

You can't map one collection navigation property (Facilities) to two relationships.

Either add another collection for one-to-many relationship:

public class User
{
    ....
    public virtual ICollection<Facility> Facilities { get; set; }
    public virtual ICollection<Facility> OwnedFacilities { get; set; }
}

and map it:

//One to Many: Facility To Owner
modelBuilder.Entity<Facility>()
    .HasRequired(s => s.Owner)
    .WithMany(s => s.OwnedFacilities) // <- another collection
    .HasForeignKey(k => k.OwnerId);

or keep the User model as it is now, but configure one-to-many relationship as unidirectional (without collection navigation property):

//One to Many: Facility To Owner
modelBuilder.Entity<Facility>()
    .HasRequired(s => s.Owner)
    .WithMany() // <- no collection
    .HasForeignKey(k => k.OwnerId);
2
5/14/2017 3:56:11 PM


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