Gina Marano
Gina Marano

Reputation: 1803

Multiplicity conflict while setting up Entity Framework

I have a more complicated question here

But thought I would simplify it.

Here are my dummied classes (the structure this is based on comes from the NDC so I don't have control over it):

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; }
}

I've tried so many things so hopefully this simplified version can help someone help me.

Over all, I want to search on:

All in all, I don't care about Linker except that is what links LeftHand to RightHand. LeftHand and RightHand are one-to-one.

    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)
            ;
    }

Thanks, I am using VS2017, EF 6.2, Code First, SQL Server

I have tried different annotations but the one common error is:

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 '*'.

What is different here than other multiplicity answers is the middle linker table.

Upvotes: 1

Views: 951

Answers (1)

TanvirArjel
TanvirArjel

Reputation: 32069

As Entity Framework 6.x does not support one-to-one relationship with explicit Foreign Key property and if you try to workaround it with ForeignKey data annotation, you'll get multiplicity error during the migration.

So only solution is: Remove RightHandId and LeftHandId properties from Linker model class as follows:

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

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

Then your model configurations should be as follows:

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"));
 }

Upvotes: 1

Related Questions