Problems creating a Foreign-Key relationship on Entity Framework(在实体框架上创建外键关系的问题)
问题描述
我在我的 Entity Framework fluent Api 中配置外键关系时遇到问题:
i'm having trouble configuring a foreign key relationship in my Entity Framework fluent Api:
这是报告的主要内容:
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");
}
报告详情:
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: : 多重性与关系Dettaglio_TEST_TABLE"中角色Dettaglio_TEST_TABLE_Target"中的引用约束冲突.由于 Dependent Role 中的所有属性都不可为空,因此 Principal Role 的复数必须为1".
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.
推荐答案
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.
这篇关于在实体框架上创建外键关系的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在实体框架上创建外键关系的问题
基础教程推荐
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 如果条件可以为空 2022-01-01
- 将数据集转换为列表 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 从 C# 控制相机设备 2022-01-01
