简体   繁体   English

在Entity Framework上创建外键关系的问题

[英]Problems creating a Foreign-Key relationship on Entity Framework

i'm having trouble configuring a foreign key relationship in my Entity Framework fluent Api: 我在我的Entity Framework流畅的Api中配置外键关系时遇到问题:

Here is the head of the report: 以下是报告的负责人:

 public class Testata
{
    public Testata() { Details = new List<Dettaglio>(); }
    public virtual int IDTEST { get; set; }
    public virtual string Value { get; set; }
    public virtual int IDDETAIL { get; set; }
    public virtual string IDTESTALT { get; set; }
    public virtual byte[] BLOB { get; set; }

    public virtual IList<Dettaglio> Details { get; set; }
}

This is the report's detail 这是报告的细节

public class Dettaglio
{
    public virtual int IDDETAIL { get; set; }
    public virtual int IDTEST { get; set; }
    public virtual string DSDETAIL { get; set; }

    public virtual Testata TEST_TABLE { get; set; }
}

And this is my fluent API definition of both. 这是我对两者的流畅API定义。 Head of the report: 报告负责人:

public TEST_TABLEMap()
    {
        // Primary Key
        this.HasKey(t => t.IDTEST)
            .Property(t => t.IDTEST)
            .IsRequired()
            .HasColumnType("Int")
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
            .HasColumnName("IDTEST");


        // Table & Column Mappings
        this.ToTable("TEST_TABLE");
        this.Property(t => t.Value).HasColumnName("DSVALUETEST");
        this.Property(t => t.IDTESTALT).HasColumnName("IDTESTALT");
        this.Property(t => t.BLOB).HasColumnName("BLOB");
    }

Detail of the report: 报告详情:

public TEST_DETAILMap()
    {
        // Primary Key
        this.HasKey(t => t.DSDETAIL);

        // Properties
        this.Property(t => t.DSDETAIL);

        // Table & Column Mappings
        this.ToTable("TEST_DETAIL");
        this.Property(t => t.IDDETAIL).HasColumnName("IDDETAIL");
        // this.Property(t => t.IDTEST).HasColumnName("IDTEST");
        this.Property(t => t.DSDETAIL).HasColumnName("DSDETAIL");

        // Relationships
        this.HasOptional(t => t.TEST_TABLE)
            .WithMany(t => t.Details)
            .HasForeignKey(d => d.IDDETAIL).WillCascadeOnDelete(true);

    }

On execution i always get this error 在执行时我总是得到这个错误

System.Data.Entity.Edm.EdmAssociationType: : Multiplicity conflicts with the referential constraint in Role 'Dettaglio_TEST_TABLE_Target' in relationship 'Dettaglio_TEST_TABLE'. System.Data.Entity.Edm.EdmAssociationType :: Multiplicity与关系'Dettaglio_TEST_TABLE'中Role'Dettaglio_TEST_TABLE_Target'中的引用约束冲突。 Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'. 由于“从属角色”中的所有属性都是不可为空的,因此“主体角色”的多重性必须为“1”。

Which, i guess, means i'm failing something at foreign key definition, but i don't really know where to look at. 我猜,这意味着我在外键定义方面失败了,但我真的不知道在哪里看。 Any help/hint is much appreciated. 非常感谢任何帮助/提示。

There is a conflict between your foreign key property in class Dettaglio ... 您在Dettaglio课程中的外键属性之间存在冲突......

public virtual int IDTEST { get; set; }

...which has a non-nullable type ( int ) and therefore cannot be optional and your mapping... ...具有非可空类型( int ),因此不能是可选的,你的映射......

this.HasOptional(t => t.TEST_TABLE) //...

...where you want the relationship to be optional. ...你希望这种关系是可选的。

If you indeed want an optional relationship use a nullable FK property: 如果您确实需要可选关系,请使用可空的FK属性:

public virtual int? IDTEST { get; set; }

Otherwise you must use HasRequired for a required relationship with a non-nullable FK property. 否则,您必须使用HasRequired获取与不可为空的FK属性的必需关系。

暂无
暂无

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

相关问题 实体框架5无法更改该关系,因为一个或多个外键属性不可为空 - Entity Framework 5 The relationship could not be changed because one or more of the foreign-key properties is non-nullable 实体框架无法更改关系,因为一个或多个外键属性不可为空 - Entity Framework The relationship could not be changed because one or more of the foreign-key properties is non-nullable 如何在带有数据注释的实体框架中检索外键关系中的列子集 - How to retrieve a subset of columns in a foreign-key relationship in Entity Framework with Data Annotations Mvc,外键关系 - Mvc, Foreign-Key Relationship 实体框架 OneToMany 更新项目 - 缺少外键值 - Entity Framework OneToMany updating items - Missing foreign-key value 尝试更新实体框架中的记录无法更改关系,因为一个或多个外键属性不可为空 - Trying to update record in entity framework The relationship could not be changed because one or more of the foreign-key properties is non-nullable 实体框架中的外键关系 - Foreign Key relationship in Entity Framework 对同一实体的外键引用 - Foreign-Key References to the Same Entity EF添加实体:由于一个或多个外键属性不可为空,因此无法更改关系 - EF adding entity: The relationship could not be changed because one or more of the foreign-key properties is non-nullable 实体框架中外键关系的空值 - Null value for the foreign key relationship in Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM