EF 4.3 Beta 1 关于可选关系级联删除留下孤儿

发布于 2024-12-29 18:38:58 字数 1015 浏览 4 评论 0原文

这很奇怪吗?

删除一对多关系时,如果该关系是可选的,并且您删除了父对象,则其余对象将保持孤立状态,并且不会级联删除。

    var album = new Album
                {
                    Name = "Test Album",
                    Description = "Test Album Description",
                    Images = new Collection<Image>
                    {
                        new Image {
                            Name = "Image 1",
                            Description = "Image 1 Description"
                        },
                        new Image {
                            Name = "Image 2",
                            Description = "Image 2Description"
                        },
                    }
                };

            albumRepository.Add(album);
            albumRepository.UnitOfWork.Commit();

在图像实体下,我将 AlbumId 设置为 Nullable,因为某些图像可能会被孤立。

然后我打电话。

albumRepository.Delete(toRemove);
albumRepository.UnitOfWork.Commit();

相册将被删除,但曾经相关的图像将被孤立,并且它们的 AlbumId 将从行中删除。

Is this odd?

When deleting relationships of one to many, if a the relationship is optional and you delete the parent object the rest will remain orphan and it does not cascade delete.

    var album = new Album
                {
                    Name = "Test Album",
                    Description = "Test Album Description",
                    Images = new Collection<Image>
                    {
                        new Image {
                            Name = "Image 1",
                            Description = "Image 1 Description"
                        },
                        new Image {
                            Name = "Image 2",
                            Description = "Image 2Description"
                        },
                    }
                };

            albumRepository.Add(album);
            albumRepository.UnitOfWork.Commit();

Under the Image Entity I got the AlbumId as Nullable since some images can be orphaned.

And then I call.

albumRepository.Delete(toRemove);
albumRepository.UnitOfWork.Commit();

The Album gets deleted but the images that where once related are Orphaned and their AlbumId is removed from the row.

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

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

发布评论

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

评论(1

梦里南柯 2025-01-05 18:38:58

这做到了。

modelBuilder.Entity<Image>()
.HasOptional(d => d.Album)
.WithMany(d => d.Images)
.WillCascadeOnDelete(true);

This did it.

modelBuilder.Entity<Image>()
.HasOptional(d => d.Album)
.WithMany(d => d.Images)
.WillCascadeOnDelete(true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文