问题在创建实体框架外键关系 [英] Problems creating a Foreign-Key relationship on Entity Framework

查看:205
本文介绍了问题在创建实体框架外键关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法配置在我的实体框架流畅API外键关系:

i'm having trouble configuring a foreign key relationship in my Entity Framework fluent 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; }
}

这是该报告的细节。

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



这是我的两个流利的API定义。该报告负责人:

And this is my fluent API definition of both. 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

\tSystem.Data.Entity.Edm.EdmAssociationType: : Multiplicity conflicts with the referential constraint in Role 'Dettaglio_TEST_TABLE_Target' in relationship 'Dettaglio_TEST_TABLE'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '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.

推荐答案

有是在课堂上你的外键属性之间的冲突 Dettaglio ...

There is a conflict between your foreign key property in class Dettaglio...

public virtual int IDTEST { get; set; }



...其中有一个非可空类型( INT ),因此不可能是可选的,你的映射...

...which has a non-nullable type (int) and therefore cannot be optional and your mapping...

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

...您想要的关系是可选的。

...where you want the relationship to be optional.

如果你真的想要一个可选的关系用一个可空FK属性:

If you indeed want an optional relationship use a nullable FK property:

public virtual int? IDTEST { get; set; }



否则,您必须使用 HasRequired 的与非空的FK属性时需要的关系。

Otherwise you must use HasRequired for a required relationship with a non-nullable FK property.

这篇关于问题在创建实体框架外键关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆