实体框架 - 反向外键检查 [英] Entity Framework - Reverse Foreign Key Check

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

问题描述

我们有一个系统,允许管理员在系统中构建新​​的内容类型,包括与其他表的外键连接。然后,管理员可以重建数据库,此时它创建表和所有必要的关系,然后重建EDMX并重新编译所有内容。像冠军一样工作(我没有写,或者我可能会知道这个答案)。

We have a system that allows Administrators to build out new content types within the system, including foreign key linkages to other tables. The Admin can then rebuild the database, at which point it creates the tables and all the necessary relationships, then rebuilds the EDMX and recompiles everything. Works like a champ (I didn't write it or I might know the answer to this).

我们有一个缺点是用户删除记录可以通过另一个表中的项目链接。由于引用完整性,这会引发错误。当然,我正在陷入困境,但我现在可以提供的一个通用的你不能删除这个项目,因为它被链接到一些类型的错误。我更希望检查项目是否可删除,如果没有,则禁用该按钮。

One drawback that we have is when a user goes to delete a record that may be linked to by an item in another table. This throws an error due to referential integrity. I'm trapping this, of course, but all I can provide right now is a generic 'You can't delete this item because it is linked to something' type of error. I would much rather check to see if the item is deletable and disable the button if not.

有没有办法可以确定哪个表/被删除的项目是否在运行时链接?通常,我只是查询相关的表格,但是由于这个应用程序的性质,我不知道在设计时这些其他表格是什么。

Is there a way that I can determine to what table/row the to-be-deleted item is linked, at runtime? Normally, I'd just query the related tables but due to the nature of this app, I don't know what those other tables would be at design-time.

所以简而言之,如果我有:

So in short, if I have:

Foo:FooID,FooName
Bar:BarID,FooID,BarName
Pow:PowID,FooID,PowName

Foo: FooID, FooName Bar: BarID, FooID, BarName Pow: PowID, FooID, PowName

有可能在运行时告诉Foo中的一行不能被删除,因为来自Bar或Pow的FK链接,如果是,那么我可以告诉哪个表导致错误?

Is it possible to tell at runtime that a row in Foo cannot be deleted due to a FK linkage from either Bar or Pow and, if so, can I then tell which table is causing the error?

提前感谢!首先在这里发帖,所以请原谅任何礼仪流感:)。

Thanks in advance; first posting here so please excuse any etiquette flubs :).

推荐答案

您必须查询元数据并获取所有导航属性的列表



You must query metadata and get list of all navigation properties

ObjectSet = context.CreateObjectSet<YourEntityType>();

// Get entity set for current entity type
var entitySet = ObjectSet.EntitySet;
// Get name of the entity's navigation properties
_propertyNames = ((EntityType)entitySet.ElementType).NavigationProperties.Select(p => p.Name).ToArray();

现在您有删除之前必须检查的属性。要检查属性,您必须加载这些成员,并且必须检查相关实体是否存在。两者都可能需要大量反思才会影响应用程序的性能。

Now you have properties which must be checked before deletion. To check properties you must load those members and you must check if related entity exists. Both will probably require a lot of reflection which will affect performance of your application.

我不得不说你的应用程序架构是可怕的。这就像一个例子:EF应该在哪里使用,或者EF如何使用。

I have to say that your application architecture is horrible. It is like an example: where should EF not be used or how should EF not be used.

这篇关于实体框架 - 反向外键检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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