从 LINQ DB 中删除是否也会删除其他具有外键关联的表中的记录?

发布于 2024-08-17 02:10:55 字数 136 浏览 4 评论 0原文

因此,在我的数据库的根目录中,我有一个表“Customer”。客户有指向大约 6-7 个其他表的外键,例如收据、地址、文档等。如果我要使用 SubmitChanges() 删除客户,是否会查找所有具有外键关联的记录并将它们也删除,或者我需要做 6 个查询吗?

So at the root of my DB, I have a table "Customer." Customer has foreign keys going to about 6-7 other tables such as Receipts, Addresses, Documents etc. If I were to delete a customer using SubmitChanges(), would that seek out all those records with the foreign key association and delete them too, or would I need to do like 6 queries?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌生 2024-08-24 02:10:55

仅当您将数据库表设置为通过级联删除(即删除级联)来执行此操作时,才会发生这种情况。

有关详细信息,请参阅插入、更新和删除操作 (LINQ to SQL)< /a>:

LINQ to SQL 不支持或
识别级联删除操作。
如果你想删除表中的一行
对它有限制的,你
必须设置 ON DELETE CASCADE
外键约束中的规则
数据库,或者使用您自己的代码
首先删除子对象
防止父对象被
已删除。否则,例外情况是
抛出。

This will only happen if you have set up your database tables to do this with cascading deletions (i.e. on delete cascade).

For more information please see Insert, Update, and Delete Operations (LINQ to SQL):

LINQ to SQL does not support or
recognize cascade-delete operations.
If you want to delete a row in a table
that has constraints against it, you
must either set the ON DELETE CASCADE
rule in the foreign-key constraint in
the database, or use your own code to
first delete the child objects that
prevent the parent object from being
deleted. Otherwise, an exception is
thrown.

零崎曲识 2024-08-24 02:10:55

您对这些外键关系采取什么样的级联行动?

默认情况下,大多数数据库系统(包括 SQL Server,我猜您使用的是 SQL Server)不会有任何“ON DELETE CASCADE”或任何其他操作 - 所以在这种情况下,什么也不会发生。

此 T-SQL 查询将显示您的外键关系以及是否已定义任何 DELETE 或 UPDATE 引用操作:

SELECT
    name 'FK Constraint',
    OBJECT_NAME(parent_object_id) 'Parent table',
    OBJECT_NAME(referenced_object_id) 'Referenced table',
    delete_referential_action ,
    delete_referential_action_desc ,
    update_referential_action ,
    update_referential_action_desc 
FROM 
    sys.foreign_keys

如果您想要发生某些事情,则需要确保设置了这些 FK 关系使用这些级联操作。

有关详细信息,请参阅有关级联引用完整性约束的 MSDN 文档。

What kind of cascade action do you have on those foreign key relations??

By default, most database systems (including SQL Server, which I presume you use) will not have any "ON DELETE CASCADE" or any other action - so in that case, nothing would happen.

This T-SQL query will show you your foreign key relationships and whether or not any DELETE or UPDATE referential actions have been defined:

SELECT
    name 'FK Constraint',
    OBJECT_NAME(parent_object_id) 'Parent table',
    OBJECT_NAME(referenced_object_id) 'Referenced table',
    delete_referential_action ,
    delete_referential_action_desc ,
    update_referential_action ,
    update_referential_action_desc 
FROM 
    sys.foreign_keys

If you want something to happen, you need to make sure those FK relations are set to use those cascading actions.

See the MSDN docs on Cascading Referential Integrity Constraints for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文