Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF 5.0 Multiplicity Error On Simple Mapping

I have the following domain objects:

public class Person 
{
    public int Id {get; set;}
    public int? FatherId {get; set;}
    public int? MotherId {get; set;}
    public int? HomeChurchId {get; set;}
    public int? BirthCountryId {get; set;}

    public Parent Father {get; set;}
    public Parent Mother {get; set;}
    public Church HomeChurch {get; set;}
    public Country BirthCountry {get; set;}
}

public class Parent
{
    public int Id {get; set;}
    ...
}

public class Church
{
    public int Id {get; set;}
    ...
}

public class Country
{
    public int Id {get; set;}
    ...
}

When mapping Person, all of these properties are mapped pretty much the same way:

HasOptional(p => p.Father).WithMany().HasForeignKey(p => p.FatherId);
HasOptional(p => p.BirthCountry).WithMany().HasForeignKey(p => p.BirthCountryId);    
...

The problem is, with BirthCountry I receive the following error when I try to query Person:

One or more validation errors were detected during model generation:

System.Data.Entity.Edm.EdmAssociationType: : Multiplicity conflicts with the
referential constraint in Role 'Person_BirthCountry_Target' in relationship
'Person_BirthCountry'. Because all of the properties in the Dependent Role are 
non-nullable, multiplicity of the Principal Role must be '1'.

If I remove the BirthCountry property (and mapping) everything works fine. What is confusing to me is that BirthCountry is set up just like every other nullable property in Person. Why aren't the other properties giving me the same error?

Thanks for whatever help you can offer!

like image 201
RHarris Avatar asked Apr 16 '13 13:04

RHarris


Video Answer


1 Answers

Ignorance is not bliss ... its just plain frustrating.

I finally realized that I had a [Required] attribute on BirthCountryId. This was causing the problem ... which totally makes sense. I can't tell EF that its optional and required at the same time.

Hope this saves someone else from the same frustration I went through.

like image 100
RHarris Avatar answered Sep 27 '22 20:09

RHarris