从 LINQ DB 中删除是否也会删除其他具有外键关联的表中的记录?
因此,在我的数据库的根目录中,我有一个表“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅当您将数据库表设置为通过级联删除(即
删除级联
)来执行此操作时,才会发生这种情况。有关详细信息,请参阅插入、更新和删除操作 (LINQ to SQL)< /a>:
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):
您对这些外键关系采取什么样的级联行动?
默认情况下,大多数数据库系统(包括 SQL Server,我猜您使用的是 SQL Server)不会有任何“ON DELETE CASCADE”或任何其他操作 - 所以在这种情况下,什么也不会发生。
此 T-SQL 查询将显示您的外键关系以及是否已定义任何 DELETE 或 UPDATE 引用操作:
如果您想要发生某些事情,则需要确保设置了这些 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:
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.