设置时多重性冲突 Entity Framework

Multiplicity conflict while setting up Entity Framework

我有一个更复杂的问题here

但我想我会简化它。

这是我的假人 类(它基于的结构来自 NDC,所以我无法控制它):

public class RightHand
{
    [Key]
    public int RightHandId { get; set; }
    public string PropertyA { get; set; }

    [Required]
    public virtual Linker Linker { get; set; }
}

public class LeftHand
{
    [Key]
    public int LeftHandId { get; set; }
    public string PropertyB { get; set; }

    [Required]
    public virtual Linker Linker { get; set; }
}

public class Linker
{
    [Key]
    public int LinkerId { get; set; }
    [ForeignKey("RightHand")]
    public int RightHandId { get; set; }
    [ForeignKey("LeftHand")]
    public int LeftHandId { get; set; }
    public string PropertyC { get; set; }

    [Required]
    public virtual RightHand RightHand  { get; set; }
    [Required]
    public virtual LeftHand LeftHand  { get; set; }
}

我已经尝试了很多东西,所以希望这个简化版本可以帮助别人帮助我。

总的来说,我想搜索:

总而言之,我不关心链接器,除了它是将 LeftHand 链接到 RightHand 的东西。 LeftHand 和 RightHand 是一对一的。

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<RightHand>()
            .Property(x => x.RightHandId)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        modelBuilder.Entity<LeftHand>()
            .Property(x => x.LeftHandId)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        modelBuilder.Entity<Linker>()
            .Property(x => x.LinkerId)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        modelBuilder.Entity<Linker>()
            .HasRequired(nus => nus.LeftHand)
            ;

        modelBuilder.Entity<Linker>()
            .HasRequired(nuu => nuu.RightHand)
            ;
    }

谢谢,我正在使用 VS2017、EF 6.2、Code First、SQL 服务器

我尝试了不同的注释,但一个常见的错误是:

Linker_LeftHand_Source: : Multiplicity is not valid in Role 'Linker_LeftHand_Source' in relationship 'Linker_LeftHand'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

Linker_RightHand_Source: : Multiplicity is not valid in Role 'Linker_RightHand_Source' in relationship 'Linker_RightHand'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

这里与其他多重答案的不同之处在于中间链接器 table。

因为 Entity Framework 6.x 不支持 one-to-one 与显式外键 属性 的关系,如果您尝试解决方法它带有 ForeignKey 数据注释,您将在迁移过程中遇到多重性错误。

所以唯一的解决方案是:从 Linker 模型 class 中删除 RightHandIdLeftHandId 属性,如下所示:

public class Linker
{
    [Key]
    public int LinkerId { get; set; }
    public string PropertyC { get; set; }

    public RightHand RightHand { get; set; }
    public LeftHand LeftHand { get; set; }
}

那么你的模型配置应该是这样的:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<LeftHand>().HasOptional(l => l.Linker)
            .WithOptionalPrincipal(lf => lf.LeftHand)
            .Map(lf => lf.MapKey("LeftHandId"));

        modelBuilder.Entity<RightHand>().HasOptional(l => l.Linker)
            .WithOptionalPrincipal(lf => lf.RightHand)
            .Map(lf => lf.MapKey("RightHandId"));
 }