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

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

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

啊啊啊啊......

最佳答案

public int ? ValueAttributeId { get; set; }

...属性需要为空。

关于c# - 使用 Fluent Api 定义可选的自引用一对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9395009/

相关文章:

c# - sql语句c#有问题

c# - 尝试确定托管您的应用程序的 DNX 进程的进程 ID 时发生错误

c# - 如何根据用户角色操作 WPF GUI

c# - 如何通过代码优先方法使用数据库迁移和 EF 4.3 创建关系表?

asp.net-mvc-3 - 管理开发和生产之间的 EF Code First 迁移

entity-framework-4.1 - 默认为 EF 4.3.1 时需要使用 EF 4.1

c# - 如何从 C# 中的视频嵌入代码中提取直接视频 URL?

c# - 当位掩码(标志)枚举变得太大时该怎么办

c# - Code First Fluent API 验证不起作用

c# - 为什么我要在 EF 中进行简单更新时遇到所有障碍?