且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在多对多关系中执行-disconnected-delete(实体框架代码优先)

更新时间:2022-11-05 09:09:09

这根本就不是可能。在具有隐藏连接实体的多对多关联中,您只能使用 独立协会 。这是没有在类模型中暴露的外键属性的关联类型: Team 竞争$ c $之间没有外键c>。

This simply isn't possible. In many-to-many associations with a hidden junction entity you can only work with independent associations. This is the type of association without a foreign key property exposed in the class model: there is no foreign key between Team and Competition.

所以你必须在删除项目之前加载一个集合。

So you have to load a collection before removing items form it.

如果你真的想为了避免这种情况,你可以做两件事情:

If you really want to avoid this you can do two things:


  • 拉接头类( CompetionTeam )进入你的模型现在您可以为路口创建存根实体,并将其标记为已删除

  • 创建第二个上下文类,其中包含 DbSet< CompetionTeam> 并使用该上下文直接操纵路口。

  • Pull the junction class (CompetionTeam) into your model. Now you can create stub entities for the junctions, and mark them as Deleted.
  • Create a second context class that has a DbSet<CompetionTeam> and use that context to manipulate junctions directly.

是不太常见的事情,但它是完全有效的。但是,将交叉点拉入你的模型可能是***的事情,因为通常,迟早的人会想要存储关于关联的信息。只有当课程是模型的一部分时才可能。

The latter option is a less common thing to do, but it's perfectly valid. But pulling the junction class into your model is probably the best thing to do, because often, sooner or later people will want to store information about the association. This is only possible when the class is part of the model.