繁体   English   中英

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

[英]Define optional self-referencing one-many relationship with Fluent Api

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

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

Aaaaahhhh .....

public int ? ValueAttributeId { get; set; }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM