使用Fluent Api定义可选的自引用一对多关系

Ian*_*ton 4 c# entity-framework-4.3

public class Attribute
{
    [Key]
    public int AttributeId { get; set; }

    [Required, StringLength(100)]
    public string Name { get; set; }

    public int ValueAttributeId { get; set; }
    public Attribute ValueAttribute { get; set; }

    public IList<Attribute> ValueAttributes { get; set; }
}

  modelBuilder.Entity<Attribute>()
     .HasOptional(a => a.ValueAttribute)
     .WithMany(a => a.ValueAttributes)
     .HasForeignKey(a => a.ValueAttributeId);
Run Code Online (Sandbox Code Playgroud)

\ tSystem.Data.Entity.Edm.EdmAssociationType ::多重性与关系'Attribute_ValueAttribute'中Role'Name_ValueAttribute_Target'中的引用约束冲突.由于"从属角色"中的所有属性都是不可为空的,因此"主体角色"的多重性必须为"1".

Aaaaahhhh .....

Ian*_*ton 10

public int ? ValueAttributeId { get; set; }
Run Code Online (Sandbox Code Playgroud)

......该属性必须是无效的.