我正在尝试删除域实体的子属性。在 UI 中,用户选择 Delete 以CustomVariableGroup
从Application
实体中删除 a。
我认为从 LINQ-to-SQL 实体中删除该属性然后提交更改,会导致 LINQ-to-SQL 处理数据库端的工作。但是该行永远不会从表中删除。当我在我的应用程序中刷新页面时,该属性仍然存在,因为它仍然存在于数据库中。
public void Save(Application application)
{
ApplicationRecord appRecord = application.Map(); // Maps domain entity to L2S entity
// Before this line executes, appRecord has 0 CustomVariableGroups, which is correct.
this.Database.ApplicationRecords.Attach(appRecord, true);
// After the attach, appRecord now has 1 CustomVariableGroup again. This is wrong.
appRecord = application.Map(); // Hack to remove the CustomVariableGroup again.
// This doesn't delete the CustomVariableGroup from appRecord. Do I need
// to delete it explicitly? Or should removing it from appRecord, and
// calling SubmitChanges() do it?
this.Database.SubmitChanges();
}
我摆脱实体上的这个子属性的正确方法是什么?我想我可以遍历列表并单独删除每个项目,但我认为 LINQ-to-SQL 不应该以这种方式工作。
任何想法表示赞赏。
注意:该属性ApplicationCustomVariableGroupRecords
表示解析many-to-many
数据库中的关联的表。一个Application
罐子有一个或多个CustomVariableGroups
,一个CustomVariableGroup
罐子属于一个或多个Applications
。