C# 使用Fluent Api定义可选的自引用一对多关系 公共类属性 { [关键] 公共int AttributeId{get;set;} [所需长度(100)] 公共字符串名称{get;set;} public int ValueAttributeId{get;set;} 公共属性值属性{get;set;} 公共IList ValueAttributes{get;set;} } modelBuilder.Entity() .has可选(a=>a.ValueAttribute) .WithMany(a=>a.ValueAttributes) .HasForeignKey(a=>a.ValueAttributeId);

C# 使用Fluent Api定义可选的自引用一对多关系 公共类属性 { [关键] 公共int AttributeId{get;set;} [所需长度(100)] 公共字符串名称{get;set;} public int ValueAttributeId{get;set;} 公共属性值属性{get;set;} 公共IList ValueAttributes{get;set;} } modelBuilder.Entity() .has可选(a=>a.ValueAttribute) .WithMany(a=>a.ValueAttributes) .HasForeignKey(a=>a.ValueAttributeId);,c#,entity-framework-4.3,C#,Entity Framework 4.3,\tSystem.Data.Entity.Edm.edmsociationType::多重性与关系“Attribute\u ValueAttribute”中角色“Attribute\u ValueAttribute\u Target”中的引用约束冲突。因为从属角色中的所有属性都不可为null,所以主体角色的多重性必须为“1” 啊啊啊 public class Attribute { [Key] public int AttributeId { get; set; } [

\tSystem.Data.Entity.Edm.edmsociationType::多重性与关系“Attribute\u ValueAttribute”中角色“Attribute\u ValueAttribute\u Target”中的引用约束冲突。因为从属角色中的所有属性都不可为null,所以主体角色的多重性必须为“1”

啊啊啊

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

。。。属性必须可以为null。

public int?ValueAttributeId{get;set;}作为备用-使用EFCF的一部分好处是,您不需要用
[Key]
[Required]
等属性装饰POCO。是的。。。这样我就可以避免从我的域引用EF。另一方面,你会想用
virtual
关键字标记这些属性。我不喜欢延迟加载加载不需要的数据和加载效率低下的可能性太大。
public int ? ValueAttributeId { get; set; }