我正在尝试实施EF解决方案,但我得到以下运行时异常:
One or more validation errors were detected during model generation:
GroupMembership_Group_Source: : Multiplicity is not valid in Role
'GroupMembership_Group_Source' in relationship 'GroupMembership_Group'. Because the
Dependent Role refers to the key properties, the upper bound of the multiplicity of the
Dependent Role must be '1'.
Gift_Membership_Source: : Multiplicity is not valid in Role
'Gift_Membership_Source' in relationship 'Gift_Membership'. Because the Dependent Role
refers to the key properties, the upper bound of the multiplicity of the Dependent Role
must be '1'.
我有以下课程:
public class Group
{
public int GroupId { get; set; }
public virtual ICollection<GroupMembership> Memberships { get; set; }
}
public class Gift
{
public int GiftId { get; set; }
public virtual GroupMembership Membership { get; set; }
}
public class GroupMembership
{
public int GroupMembershipId { get; set; }
public virtual ICollection<Gift> Gifts { get; set; }
public virtual Group Group { get; set; }
}
以下配置(为简洁起见缩短了):
public class GroupConfiguration : EntityTypeConfiguration<Group>
{
public GroupConfiguration()
{
HasMany(x => x.Memberships).WithRequired(x => x.Group).HasForeignKey(x => x.GroupMembershipId);
}
}
public class GiftConfiguration : EntityTypeConfiguration<Gift>
{
public GiftConfiguration()
{
HasRequired(x => x.Membership).WithMany(x => x.Gifts).HasForeignKey(x => x.GiftId);
}
}
public class GroupMembershipConfiguration : EntityTypeConfiguration<GroupMembership>
{
public GroupMembershipConfiguration()
{
HasRequired(x => x.Group).WithMany(x => x.Memberships).HasForeignKey(x => x.GroupMembershipId);
HasMany(x => x.Gifts).WithRequired(x => x.Membership).HasForeignKey(x => x.GiftId);
}
}
提前致谢
编辑:有关礼品/用户关系的其他例外......
public class Gift
{
public int GiftId { get; set; }
public int ClaimedByUserId { get; set; }
public virtual User ClaimedByUser { get; set; }
}
public class User
{
public int UserId { get; set; }
}
public class GiftConfiguration : EntityTypeConfiguration<Gift>
{
public GiftConfiguration()
{
HasOptional(x => x.ClaimedByUser).WithOptionalPrincipal();
}
}
它不喜欢映射。我得到Invalid column name 'ClaimedByUser_UserId'.
答案 0 :(得分:2)
您的模型中存在一些问题。如果您要在GroupMembership
和Group
之间配置一对多关系,那么您的模型应该是这样的:
public class Group
{
public int GroupId { get; set; }
public virtual ICollection<GroupMembership> Memberships { get; set; }
}
public class GroupMembership
{
public int GroupMembershipId { get; set; }
public virtual ICollection<Gift> Gifts { get; set; }
public int GroupId {get;set;} //Add this FK property
public virtual Group Group { get; set; }
}
您的Fluent Api配置将是:
public GroupConfiguration()
{
HasMany(x => x.Memberships).WithRequired(x => x.Group).HasForeignKey(x => x.GroupId);
}
问题是你在一对多的关系中使用实体的PK作为FK,出了什么问题。第二个关系也是如此,Gift
和GroupMembership
之间的关系也是如此(在Gift
实体中添加FK属性):
public class Gift
{
public int GiftId { get; set; }
public int GroupMembershipId { get; set; } //Add this FK property
public virtual GroupMembership Membership { get; set; }
}
配置:
HasRequired(x => x.Membership).WithMany(x => x.Gifts).HasForeignKey(x => x.GroupMembershipId);
此外,您不需要在GroupMembershipConfiguration
类中重复两种关系的配置(删除它们)。一旦足够就可以了。
尝试使用此配置:
HasOptional(x => x.ClaimedByUser).WithMany().HasForeignKey(g=>g.ClaimedByUserId );
我认为这是另一种一对多的关系,因为一个用户可以与一个或多个礼物相关联。