RHa*_*ris 5 entity-framework code-first
我有以下域对象:
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;}
    ...
}
映射Person时,所有这些属性的映射方式几乎相同:
HasOptional(p => p.Father).WithMany().HasForeignKey(p => p.FatherId);
HasOptional(p => p.BirthCountry).WithMany().HasForeignKey(p => p.BirthCountryId);    
...
问题是,使用BirthCountry,当我尝试查询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'.
如果我删除BirthCountry属性(和映射)一切正常.令我困惑的是,BirthCountry的设置就像Person中的其他所有可空属性一样.为什么其他属性没有给我同样的错误?
感谢您提供的任何帮助!
RHa*_*ris 11
无知并不是幸福......它只是令人沮丧.
我终于意识到我在BirthCountryId上有一个[Required]属性.这导致了问题......这完全有道理.我无法告诉EF它的可选和同时需要.
希望这可以拯救别人免受同样的挫折感.
| 归档时间: | 
 | 
| 查看次数: | 6188 次 | 
| 最近记录: |