EF 6 Fluent API 具有一对多和多对多以及相互冲突的多重性
EF 6 Fluent API with One to Many AND Many To Many and conflicting multiplicities
我没能找到这个具体问题的答案,也许是因为我对 Entity Framework
很陌生
我有两个实体:用户和设施。
设施 table 需要一个所有者(用户),但一个设施也可以有多个 "FacilityUsers" 分配给一个设施。
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; }
}
这是我的模型构建器
////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);
问题是,当我有地图 Facility to Owner 时,我收到以下错误:The navigation 属性 'FacilityUsers' 在类型 'MEH.Web.Models.Entities.Facility' 上声明已配置有冲突的多重性 。
但是,当我排除 Facility to Owner 映射时,它起作用了。
//One to Many: Facility To Owner
//modelBuilder.Entity<Facility>()
// .HasRequired<User>(s => s.Owner)
// .WithMany(s => s.Facilities)
// .HasForeignKey(k => k.OwnerId);
问题是,Facilities table 然后有一个 Owner_UserId 字段为空,但是OwnerId 已正确填写。在我解决 "Owner_UserId" 问题之前,我的强迫症不会让我继续前进。
谢谢,
D
您不能将一个 集合导航属性 (Facilities
) 映射到两个 关系。
为 one-to-many
关系添加另一个集合:
public class User
{
....
public virtual ICollection<Facility> Facilities { get; set; }
public virtual ICollection<Facility> OwnedFacilities { get; set; }
}
并映射它:
//One to Many: Facility To Owner
modelBuilder.Entity<Facility>()
.HasRequired(s => s.Owner)
.WithMany(s => s.OwnedFacilities) // <- another collection
.HasForeignKey(k => k.OwnerId);
或者保持现在的User
模型,但是将one-to-many
关系配置为单向(没有集合导航属性):
//One to Many: Facility To Owner
modelBuilder.Entity<Facility>()
.HasRequired(s => s.Owner)
.WithMany() // <- no collection
.HasForeignKey(k => k.OwnerId);
我没能找到这个具体问题的答案,也许是因为我对 Entity Framework
很陌生我有两个实体:用户和设施。
设施 table 需要一个所有者(用户),但一个设施也可以有多个 "FacilityUsers" 分配给一个设施。
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; }
}
这是我的模型构建器
////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);
问题是,当我有地图 Facility to Owner 时,我收到以下错误:The navigation 属性 'FacilityUsers' 在类型 'MEH.Web.Models.Entities.Facility' 上声明已配置有冲突的多重性 。
但是,当我排除 Facility to Owner 映射时,它起作用了。
//One to Many: Facility To Owner
//modelBuilder.Entity<Facility>()
// .HasRequired<User>(s => s.Owner)
// .WithMany(s => s.Facilities)
// .HasForeignKey(k => k.OwnerId);
问题是,Facilities table 然后有一个 Owner_UserId 字段为空,但是OwnerId 已正确填写。在我解决 "Owner_UserId" 问题之前,我的强迫症不会让我继续前进。
谢谢, D
您不能将一个 集合导航属性 (Facilities
) 映射到两个 关系。
为 one-to-many
关系添加另一个集合:
public class User
{
....
public virtual ICollection<Facility> Facilities { get; set; }
public virtual ICollection<Facility> OwnedFacilities { get; set; }
}
并映射它:
//One to Many: Facility To Owner
modelBuilder.Entity<Facility>()
.HasRequired(s => s.Owner)
.WithMany(s => s.OwnedFacilities) // <- another collection
.HasForeignKey(k => k.OwnerId);
或者保持现在的User
模型,但是将one-to-many
关系配置为单向(没有集合导航属性):
//One to Many: Facility To Owner
modelBuilder.Entity<Facility>()
.HasRequired(s => s.Owner)
.WithMany() // <- no collection
.HasForeignKey(k => k.OwnerId);