删除 LINQ to EF 中的关联数据

发布于 2024-10-11 02:26:00 字数 549 浏览 2 评论 0原文

我是 LINQ 和 EF 的新手,我有一个关于我正在开发的论坛的快速问题。这个论坛上有主题,每个主题都有相关的回复。我假设 EF 会看到该约束,并且在删除主题时也会删除关联的回复。相反,它会引发约束错误。有没有一种简单的方法可以删除所有关联的回复,而无需循环遍历它们并将每个回复标记为删除?

例如,在 SQL 中我只会做这样的事情:

DELETE FROM topic_replies WHERE TopicID='999'
DELETE FROM topics where TopicID='999'

但在 EF 中,我知道这样做的唯一方法是:

Topic topic = //LINQ to get topic.
foreach (Reply reply in topic.Replies)
{
     dbEntity.Replies.DeleteObject(reply);
}
dbEntity.Topics.DeleteObject(topic);

如果这是我必须做的,我想这很好。只是好奇是否有更好的方法。提前致谢。

I am new to LINQ and EF and I have a quick question about a forum I am developing. On this forum there are topics, and each topic has associated replies. I assumed that EF would see the constraint and when deleting the topic would also delete the associated replies. Instead it throws a constraint error. Is there an easy way to delete all associated replies without looping through them and marking each one for deletion?

For instance, in SQL I would just do something like this:

DELETE FROM topic_replies WHERE TopicID='999'
DELETE FROM topics where TopicID='999'

But in EF, the only way I know to do this is:

Topic topic = //LINQ to get topic.
foreach (Reply reply in topic.Replies)
{
     dbEntity.Replies.DeleteObject(reply);
}
dbEntity.Topics.DeleteObject(topic);

I guess this is fine if this is what I have to do. Just curious if there is a better way. Thanks in advance.

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

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

发布评论

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

评论(3

尤怨 2024-10-18 02:26:01

您可以在 EF 架构中的所有者对象中设置级联删除。

<Association Name="FK_ItemChildren_Item">
 <End Role="Item" Type="Model.Store.Item" Multiplicity="1">
  <OnDelete Action="Cascade" />
 </End>
 <End Role="ItemChildren" Type="Model.Store.ItemChildren"
  Multiplicity="*" />
 <ReferentialConstraint>
 ..
 </ReferentialConstraint>
</Association>

看看 http://codepolice.net/2008/12/ 16/级联删除实体框架/

You can set up cascade on delete in owner object in EF schema.

<Association Name="FK_ItemChildren_Item">
 <End Role="Item" Type="Model.Store.Item" Multiplicity="1">
  <OnDelete Action="Cascade" />
 </End>
 <End Role="ItemChildren" Type="Model.Store.ItemChildren"
  Multiplicity="*" />
 <ReferentialConstraint>
 ..
 </ReferentialConstraint>
</Association>

Look at http://codepolice.net/2008/12/16/cascade-delete-in-entity-framework/

在您的数据库中级联删除这些表...然后,当您删除父表时,数据库将处理子表的删除。

级联是外键的一部分

示例

如果您使用 SQL Server

ALTER TABLE dbo.PersonAddress ADD CONSTRAINT
    FK_PersonAddress_Address FOREIGN KEY
    (
    AddressId
    ) REFERENCES dbo.Address
    (
    AddressId
    ) ON UPDATE  NO ACTION 
     ON DELETE  CASCADE   -- Here is where you set your Update and Delete Actions for the foreign key constraint.

现在基本上就是这样的;如果我删除一个人,那么数据库将删除与该人相关的所有地址

In your database Cascade your deletes for these tables...Then when you delete the parent the database will handle the deletion of the child.

Cascading is part of the foreign key

Example

If your using SQL Server

ALTER TABLE dbo.PersonAddress ADD CONSTRAINT
    FK_PersonAddress_Address FOREIGN KEY
    (
    AddressId
    ) REFERENCES dbo.Address
    (
    AddressId
    ) ON UPDATE  NO ACTION 
     ON DELETE  CASCADE   -- Here is where you set your Update and Delete Actions for the foreign key constraint.

Now basically what this says is; If i delete a person then the database will delete all addresses that are related to that person

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