Entity Framework Core 7 外键

huangapple go评论59阅读模式
英文:

Entity Framework Core 7 Foreign Key

问题

我在我的实体中有一个外键 "string ParamName",它引用 ParamDesc 实体。我如何使这个外键不是严格的?我的意思是,我希望我的外键可以包含任何值,即使不引用任何 ParamDesc 实体。提前谢谢。

我已经尝试过:

modelBuilder
    .Entity<ParamInfo>()
    .HasOne(pi => pi.ParamDesc)
    .WithMany(e => e.ParamInfos)
    .HasForeignKey(pi => pi.ParamName)
    .IsRequired(false)
    .OnDelete(DeleteBehavior.NoAction);

但我仍然无法添加具有未知 ParamName 的 ParamInfo,但我想要能够这样做。

英文:

I have a Foreign Key "string ParamName" in my entity, it refers to ParamDesc entity. How can I make this foreign key not strict? I mean, I want my foreign key can contain any values, even that not refer to any ParamDesc entity. Thank you in advance.

I have tried:

modelBuilder
                .Entity&lt;ParamInfo&gt;()
                .HasOne(pi =&gt; pi.ParamDesc)
                .WithMany(e =&gt; e.ParamInfos)
                .HasForeignKey(pi =&gt; pi.ParamName)
                .IsRequired(false)
                .OnDelete(DeleteBehavior.NoAction);

I still cannot add ParamInfo with unknown ParamName, but I want to.

答案1

得分: 2

我是说,我希望我的外键可以包含任何值,即使它们不引用任何ParamDesc实体

那么这不是一个外键,也不应该是一个外键。这不是EF Core的严格规定,而是关系数据库中外键的定义:

来自SQL Server文档

外键(FK)是用于建立和强制两个表中数据之间链接的列或列组合,以控制可以存储在外键表中的数据。

或者维基百科

外键是表中一组属性,它们引用另一张表的主键。外键将这两张表关联起来。换句话说:在关系数据库的上下文中,外键是一组属性,受到某种类型的包含依赖约束的约束,具体而言,是关于在一个关系R中由外键属性组成的元组,也必须存在于另一个(不一定是不同的)关系S中,而且这些属性还必须是S中的候选键。

因此,它们用于维护引用一致性,而您的列 - 不是。要么将其拆分成两个列,一个用于可选的外键(带有可选关系),一个用于其余数据,要么添加另一个列,该列将确定存储在第一个列中的数据类型,并根据该列执行“手动”连接(我建议遵循第一种方法)。

英文:

> I mean, I want my foreign key can contain any values, even that not refer to any ParamDesc entity

Then it is not a foreign key and should not be one. That is not a strict EF Core rule but the definition of foreign key in relational databases:

From SQL Server docs:

> A foreign key (FK) is a column or combination of columns that is used to establish and enforce a link between the data in two tables to control the data that can be stored in the foreign key table.

Or Wiki:

> A foreign key is a set of attributes in a table that refers to the primary key of another table. The foreign key links these two tables. Another way to put it: In the context of relational databases, a foreign key is a set of attributes subject to a certain kind of inclusion dependency constraints, specifically a constraint that the tuples consisting of the foreign key attributes in one relation, R, must also exist in some other (not necessarily distinct) relation, S, and furthermore that those attributes must also be a candidate key in S

So they are used for referential consistency, and your column - is not. Either split it in two, one for optional FK (with optional relationship) and one for the rest of data or add another column which will determine the type of data stored in the first one and use "manual" joins based on that (I would recommend following the first approach).

huangapple
  • 本文由 发表于 2023年7月10日 18:08:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652713.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定