Entity framework 流畅映射多对多
Entity framework fluent mapping many-to-may
我在尝试映射此模型时遇到问题:
public class Discipline
{
public int Id {get;set;}
public string Name{get;set;}
public virtual ICollection<DisciplineRequirement> Requirements {get;set;}
}
public class DisciplineRequirement
{
public int DisciplineId {get;set;}
public int RequiredDiscipline {get;set;}
public virtual Discipline Discipline {get;set;}
public virtual Discipline RequiredDiscipline {get;set;}
}
映射代码:
public class DisciplineMap : EntityTypeConfiguration<Discipline>
{
public DisciplineMap()
{
ToTable("Discipline");
HasKey(p => p.Id);
HasMany(p => p.Requirements)
.WithRequired(p => p.Discipline)
.HasForeignKey(p => p.DisciplineId);
HasMany(p => p.Requirements)
.WithRequired(p => p.RequiredDiscipline)
.HasForeignKey(p => p.RequiredDisciplineId);
}
}
public class DisciplineRequirementMap : EntityTypeConfiguration<DisciplineRequeriment>
{
public DisciplineRequirementMap()
{
ToTable("DisciplineRequirement");
HasKey(p => new
{
p.DisciplineId,
p.RequiredDisciplineId
});
HasRequired(p => p.Discipline)
.WithMany(p => p.Requirements)
.HasForeignKey(p => p.DisciplineId);
HasRequired(p => p.RequiredDiscipline)
.WithMany(p => p.Requirements)
.HasForeignKey(p => p.RequiredDisciplineId);
}
}
我遇到了这个错误:
在模型生成过程中检测到一个或多个验证错误:
要求:FromRole:NavigationProperty 'Requirements' 无效。 AssociationType 'Discipline_Requirements' 中 FromRole 'Discipline_Requirements_Target' 的类型 'DisciplineRequeriment' 必须与声明此 NavigationProperty 的类型 'Discipline' 完全匹配。
有人可以给我点灯吗?
恕我直言,你必须有两个导航属性(或者至少如果只有一个你不能用两个 FK 配置它)
public class Discipline
{
public int Id {get;set;}
public string Name{get;set;}
public virtual ICollection<DisciplineRequirement> Requirements {get;set;}
public virtual ICollection<DisciplineRequirement> RequiredBy {get;set;}
}
public class DisciplineRequirement
{
public int DisciplineId {get;set;}
public int RequiredDisciplineId {get;set;}
public virtual Discipline Discipline {get;set;}
public virtual Discipline RequiredDiscipline {get;set;}
}
和
public class DisciplineMap : EntityTypeConfiguration<Discipline>
{
public DisciplineMap()
{
ToTable("Discipline");
HasKey(p => p.Id);
}
}
public class DisciplineRequirementMap : EntityTypeConfiguration<DisciplineRequeriment>
{
public DisciplineRequirementMap()
{
ToTable("DisciplineRequirement");
HasKey(p => new
{
p.DisciplineId,
p.RequiredDisciplineId
});
HasRequired(p => p.Discipline)
.WithMany(p => p.RequiredBy)
.HasForeignKey(p => p.DisciplineId);
HasRequired(p => p.RequiredDiscipline)
.WithMany(p => p.Requirements)
.HasForeignKey(p => p.RequiredDisciplineId);
}
}
应该够了。
我在尝试映射此模型时遇到问题:
public class Discipline
{
public int Id {get;set;}
public string Name{get;set;}
public virtual ICollection<DisciplineRequirement> Requirements {get;set;}
}
public class DisciplineRequirement
{
public int DisciplineId {get;set;}
public int RequiredDiscipline {get;set;}
public virtual Discipline Discipline {get;set;}
public virtual Discipline RequiredDiscipline {get;set;}
}
映射代码:
public class DisciplineMap : EntityTypeConfiguration<Discipline>
{
public DisciplineMap()
{
ToTable("Discipline");
HasKey(p => p.Id);
HasMany(p => p.Requirements)
.WithRequired(p => p.Discipline)
.HasForeignKey(p => p.DisciplineId);
HasMany(p => p.Requirements)
.WithRequired(p => p.RequiredDiscipline)
.HasForeignKey(p => p.RequiredDisciplineId);
}
}
public class DisciplineRequirementMap : EntityTypeConfiguration<DisciplineRequeriment>
{
public DisciplineRequirementMap()
{
ToTable("DisciplineRequirement");
HasKey(p => new
{
p.DisciplineId,
p.RequiredDisciplineId
});
HasRequired(p => p.Discipline)
.WithMany(p => p.Requirements)
.HasForeignKey(p => p.DisciplineId);
HasRequired(p => p.RequiredDiscipline)
.WithMany(p => p.Requirements)
.HasForeignKey(p => p.RequiredDisciplineId);
}
}
我遇到了这个错误:
在模型生成过程中检测到一个或多个验证错误: 要求:FromRole:NavigationProperty 'Requirements' 无效。 AssociationType 'Discipline_Requirements' 中 FromRole 'Discipline_Requirements_Target' 的类型 'DisciplineRequeriment' 必须与声明此 NavigationProperty 的类型 'Discipline' 完全匹配。
有人可以给我点灯吗?
恕我直言,你必须有两个导航属性(或者至少如果只有一个你不能用两个 FK 配置它)
public class Discipline
{
public int Id {get;set;}
public string Name{get;set;}
public virtual ICollection<DisciplineRequirement> Requirements {get;set;}
public virtual ICollection<DisciplineRequirement> RequiredBy {get;set;}
}
public class DisciplineRequirement
{
public int DisciplineId {get;set;}
public int RequiredDisciplineId {get;set;}
public virtual Discipline Discipline {get;set;}
public virtual Discipline RequiredDiscipline {get;set;}
}
和
public class DisciplineMap : EntityTypeConfiguration<Discipline>
{
public DisciplineMap()
{
ToTable("Discipline");
HasKey(p => p.Id);
}
}
public class DisciplineRequirementMap : EntityTypeConfiguration<DisciplineRequeriment>
{
public DisciplineRequirementMap()
{
ToTable("DisciplineRequirement");
HasKey(p => new
{
p.DisciplineId,
p.RequiredDisciplineId
});
HasRequired(p => p.Discipline)
.WithMany(p => p.RequiredBy)
.HasForeignKey(p => p.DisciplineId);
HasRequired(p => p.RequiredDiscipline)
.WithMany(p => p.Requirements)
.HasForeignKey(p => p.RequiredDisciplineId);
}
}
应该够了。