在实体框架上建立外键关系的问题 [英] Problems creating a Foreign-Key relationship on Entity Framework
问题描述
以下是报告的负责人:
public class Testata
{
public Testata(){Details = new List< Dettaglio>(); }
public virtual int IDTEST {get;组; }
public virtual string Value {get;组; }
public virtual int IDDETAIL {get;组; }
public virtual string IDTE组; }
public virtual byte [] BLOB {get;组; }
public virtual IList< Dettaglio>细节{get;组;
}
这是报表的细节
public class Dettaglio
{
public virtual int IDDETAIL {get;组; }
public virtual int IDTEST {get;组; }
public virtual string DSDETAIL {get;组; }
public virtual Testata TEST_TABLE {get;组; }
}
这是我这两个流畅的API定义。报告负责人:
public TEST_TABLEMap()
{
//主键
this.HasKey(t => t.IDTEST)
.Property(t => t.IDTEST)
.IsRequired()
.HasColumnType(Int)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.HasColumnName(IDTEST);
// Table&列映射
this.ToTable(TEST_TABLE);
this.Property(t => t.Value).HasColumnName(DSVALUETEST);
this.Property(t => t.IDTE台).HasColumnName(IDTE台);
this.Property(t => t.BLOB).HasColumnName(BLOB);
}
报表详细信息:
public TEST_DETAILMap()
{
//主键
this.HasKey(t => t.DSDETAIL);
//属性
this.Property(t => t.DSDETAIL);
// Table&列映射
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);
//关系
this.HasOptional(t => t.TEST_TABLE)
.WithMany(t => t.Details)
.HasForeignKey(d = d.IDDETAIL).WillCascadeOnDelete(true);
}
执行时我总是得到这个错误
System.Data.Entity.Edm.EdmAssociationType::与Dettaglio_TEST_TABLE关系中角色Dettaglio_TEST_TABLE_Target中的参照约束的多重性冲突。因为从属角色中的所有属性都不可为空,所以主体角色的多重性必须为1。
我猜,这意味着我在外键定义方面失败了,但我真的不知道在哪里看。
任何帮助/提示都不胜感激。
您的外键属性在 Dettaglio
...
public virtual int IDTEST {get;组; }
...它有一个不可空的类型( int
),因此不能是可选的,并且您的映射...
this.HasOptional(t => t .TEST_TABLE)// ...
...您希望关系是可选的。 / p>
如果您确实想要一个可选关系使用可空的FK属性:
public virtual int? IDTEST {get;组;
否则,您必须使用 HasRequired
需要与不可空FK属性的关系。
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; }
}
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. 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'. 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.
There is a conflict between your foreign key property in class Dettaglio
...
public virtual int IDTEST { get; set; }
...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.
If you indeed want an optional relationship use a nullable FK property:
public virtual int? IDTEST { get; set; }
Otherwise you must use HasRequired
for a required relationship with a non-nullable FK property.
这篇关于在实体框架上建立外键关系的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!