简体   繁体   English

与EntityFramework中的独立关联一起实现“公用”外键

[英]Implement a “Common” foreign key with Independent Association in EntityFramework

I am using EntityFramework 6 in my project, which uses a AuditLog table to keep track of changes in multiple other tables (to simply, I name them T1, T2,... T5 without the details). 我在我的项目中使用EntityFramework 6,该项目使用AuditLog表来跟踪多个其他表中的更改(简单地说,我将它们命名为T1,T2,... T5,但没有详细信息)。 Every record in AuditLog table is for one of the other table (T1, ... T5). AuditLog表中的每个记录都是另一个表(T1,... T5)中的一个。 So it is a [0..1] to many relationship. 因此,这是[0..1]的许多关系。

I do not want to use the standard FK constraint based association, because it would introduce 5 FK in the AuditLog table, and 4 out of the 5 FK columns are null for each record. 我不想使用基于标准FK约束的关联,因为它将在AuditLog表中引入5 FK,并且5条FK列中的4条对于每条记录都是空的。 I'd like to use a "common" column in the AuditLog table to serve as a "common" FK to other tables, the value in this FK column could be the primary key from one of the 5 tables. 我想使用AuditLog表中的“公共”列作为其他表的“公共” FK,此FK列中的值可以是5个表之一中的主键。 As a result, the following SQL is generated for AuditLog table: 结果,为AuditLog表生成了以下SQL:

CONSTRAINT [FK_AuditLogT1] FOREIGN KEY ([EntityId]) REFERENCES [dbo].[T1] ([Id]),
CONSTRAINT [FK_AuditLogT2] FOREIGN KEY ([EntityId]) REFERENCES [dbo].[T2] ([Id]),
CONSTRAINT [FK_AuditLogT3] FOREIGN KEY ([EntityId]) REFERENCES [dbo].[T3] ([Id]),
CONSTRAINT [FK_AuditLogT4] FOREIGN KEY ([EntityId]) REFERENCES [dbo].[T4] ([Id]),
CONSTRAINT [FK_AuditLogT5] FOREIGN KEY ([EntityId]) REFERENCES [dbo].[T5] ([Id])

At runtime, when the app is adding new objects of type T1(or T2...T5), the EF would throw the following exception due to the FK constraint: 在运行时,当应用程序添加类型为T1(或T2 ... T5)的新对象时,由于FK约束,EF将引发以下异常:

Exception: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_AuditT2". 例外:INSERT语句与FOREIGN KEY约束“ FK_AuditT2”冲突。 The conflict occurred in database "xxxx", table "dbo.T2", column 'Id'. 数据库“ xxxx”的表“ dbo.T2”的列“ Id”中发生了冲突。

I have tried to use independent association to map the relationship between AuditLog and other tables, there is no exception, but, EF will not auto-populate the entityId column in AuditLog table with the primary key of the other tables. 我尝试使用独立关联来映射AuditLog与其他表之间的关系,没有例外,但是EF不会使用其他表的主键自动填充AuditLog表中的entityId列。

Is there a way I can use the independent association but also maintain the primary key of the other tables in the entityId in AuditLog? 有没有一种方法可以使用独立关联,还可以在AuditLog的entityId中维护其他表的主键?

You cannot do this with Entity Framework associations - this is a database limitation - a field cannot be in multiple relationships as the constraints will conflict (eg T1 has key 10, but this isn't present in T2, therefore the constraints on your "Key" field will be broken. 您不能使用Entity Framework关联来执行此操作-这是数据库的限制-字段不能处于多个关系中,因为约束将发生冲突(例如T1的键为10,但T2中不存在此键,因此对“键”的约束字段将被破坏。

You've not said what you want to achieve in your audit log, but there are a number of efforts around to add an automatic audit log for Entity Framework entities - these would be simpler because you wouldn't have to explicitly add them, but may not fill all of your requirements. 您没有在审计日志中说出要实现的目标,但是围绕着为Entity Framework实体添加自动审计日志进行了许多工作-这些将更加简单,因为您不必显式添加它们,但是可能无法满足您的所有要求。 Some of these include: 其中一些包括:

Plus a number of blog posts found by searching for Entity Framework Audit Log 加上通过搜索Entity Framework Audit Log找到的许多博客文章

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM