简体   繁体   English

Linq to SQL-删除子属性

[英]Linq to SQL - Delete Child Property

I'm trying to delete a child property of a domain entity. 我正在尝试删除域实体的子属性。 In the UI, the user selects Delete to remove a CustomVariableGroup from an Application entity. 在UI中,用户选择“删除”以从Application实体中删除CustomVariableGroup

I thought deleting the property from the LINQ-to-SQL entity & then submitting changes, would cause LINQ-to-SQL to take care of the work on the Database side. 我认为从LINQ-to-SQL实体中删除该属性,然后提交更改,将导致LINQ-to-SQL负责数据库方面的工作。 But the row never gets deleted from the table. 但是该行永远不会从表中删除。 When I refresh the page in my application, the property is still there because it's still there in the Database. 当我刷新应用程序中的页面时,该属性仍然存在,因为它仍在数据库中。

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();
}

What is the right way for me to get rid of this child property on the entity? 我如何摆脱实体上的此子财产的正确方法是什么? I guess I could loop through the list and delete each item individually, but I don't think LINQ-to-SQL is supposed to work that way. 我想我可以遍历列表并单独删除每个项目,但是我认为LINQ-to-SQL不能那样工作。

Any ideas are appreciated. 任何想法表示赞赏。

Note: The property ApplicationCustomVariableGroupRecords represents a table that resolves a many-to-many association in the Database. 注意:属性ApplicationCustomVariableGroupRecords表示一个表,用于解析数据库中many-to-many关联。 An Application can have one or more CustomVariableGroups , and a CustomVariableGroup can belong to one or more Applications . 一个Application可以具有一个或多个CustomVariableGroups ,而一个CustomVariableGroup可以属于一个或多个Applications

Normally you have to specifically delete the object - removing it from a parent collection just means you don't want it to be associated with that particular parent anymore. 通常,您必须专门删除该对象-从父集合中删除该对象仅意味着您不再希望将其与该特定父对象关联。 It can't tell that you don't want to then associate it with a different parent. 它不能告诉您您不想再将其与其他父项关联。 If you want it to get deleted, you need to make the call to have it deleted (DeleteOnSubmit for L2S, IIRC) 如果要删除它,则需要进行呼叫以将其删除(L2S,IIRC的DeleteDeleteOnSubmit)

if im not wrong the tables which have n to n relations between them are works like nested..So try to first delete from the 3rd table (which contains IDs of 2 table) and then remove from main table.. 如果我没看错,那么它们之间具有n到n关系的表就像嵌套一样工作。因此,请尝试首先从第3个表(包含2个表的ID)中删除,然后从主表中删除。

[Sorry, i can't see add comment button on the page.. so i wrote this idea as answer ] [抱歉,我在页面上看不到添加评论按钮。。所以我将此想法写为答案]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM