0

I am receiving this error in my migrations:

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

Here are my models (note: base entity holds the primary key for all models):

public class EventModerator : BaseEntity
{
    ......

    // foreign keys
    public int PersonId { get; set; }

    // associations
    [ForeignKey("PersonId")]
    public Person Person { get; set; }

}


 public class Person : BaseEntity
  {
    public Person()
    {
    ....
    // association
    public virtual EventModerator EventModerator { get; set; }

}

My Mappings:

        modelBuilder.Entity<Person>()
            .HasOptional(e => e.EventModerator)
            .WithRequired(e => e.Person);

This is a 1 to 0.1 relationship.

Can anyone point out my error please?

2 Answers2

0

OK, this worked, but frankly I do not understand the need for ".WithMany()"

    internal static void Map(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<EventModerator>()
            .HasRequired(e => e.Person)
            .WithMany()
            .HasForeignKey(e => e.PersonId)
            .WillCascadeOnDelete(false);   
    }
0

Your answer will not produce 1 to 0.1 relationship. There is another key generated on Person table in the database, that is nullable EventModerator_Id.

To have 1 to 0.1, the dependent EventModerator primary key must also be the foreign key.

You can either add [Key] attribute on PersonId.

[Key]
public int PersonId { get; set; }

Or since you have BaseEntity which might have derived Id property (which by default convention is a primary key), then you just need to remove the PersonId property and link foreign key to Id property.

//public int PersonId { get; set; }

// associations
[ForeignKey("Id")]
public Person Person { get; set; }
Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67