如何从 LINQ to SQL 中的子集合中删除记录? [英] How do I delete records from a child collection in LINQ to SQL?

查看:11
本文介绍了如何从 LINQ to SQL 中的子集合中删除记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有两个表由外键连接:Page(PageId,其他数据)和 PageTag(PageId,Tag).我使用 LINQ 为这些表生成类,页面作为父集合,标签作为子集合(一对多关系).有没有办法从 Page 类中标记 PageTag 记录从数据库中删除?

I have two tables in my database connected by foreign keys: Page (PageId, other data) and PageTag (PageId, Tag). I've used LINQ to generate classes for these tables, with the page as the parent and the Tag as the child collection (one to many relationship). Is there any way to mark PageTag records for deletion from the database from within the Page class?

快速清除:

我希望在父 DataContext 调用 SubmitChanges() 时删除子对象,而不是之前.我希望 TagString 的行为与 Page 对象的任何其他属性完全一样.

I want the child objects to be deleted when the parent DataContext calls SubmitChanges(), not before. I want TagString to behave exactly like any of the other properties of the Page object.

我想启用如下代码:

Page page = mDataContext.Pages.Where(page => page.pageId = 1);
page.TagString = "new set of tags";

//Changes have not been written to the database at this point.

mDataContext.SubmitChanges();

//All changes should now be saved to the database.

以下是我的详细情况:
为了更轻松地处理标签集合,我向 Page 对象添加了一个属性,该属性将标签集合视为字符串:

Here is my situation in detail:
In order to make working with the collection of tags easier, I've added a property to the Page object that treats the Tag collection as a string:

public string TagString {
    get {
        StringBuilder output = new StringBuilder();
        foreach (PageTag tag in PageTags) {
            output.Append(tag.Tag + " ");
        }

        if (output.Length > 0) {
            output.Remove(output.Length - 1, 1);
        }

        return output.ToString();
    }
    set {
        string[] tags = value.Split(' ');
        PageTags.Clear();
        foreach (string tag in tags) {
            PageTag pageTag = new PageTag();
            pageTag.Tag = tag;
            PageTags.Add(pageTag);
        }
    }
}

基本上,这个想法是当一串标签被发送到这个属性时,对象的当前标签被删除并在它们的位置生成一个新的集合.

Basically, the idea is that when a string of tags is sent to this property, the current tags of the object are deleted and a new set is generated in their place.

我遇到的问题是这一行:

The problem I'm encountering is that this line:

PageTags.Clear();

提交更改时实际上不会从数据库中删除旧标签.

Doesn't actually delete the old tags from the database when changes are submitted.

环顾四周,删除东西的正确"方法似乎是调用数据上下文类的DeleteOnSubmit方法.但我似乎无法从 Page 类中访问 DataContext 类.

Looking around, the "proper" way to delete things seems to be to call the DeleteOnSubmit method of the data context class. But I don't appear to have access to the DataContext class from within the Page class.

有谁知道从 Page 类中标记要从数据库中删除的子元素的方法吗?

Does anyone know of a way to mark the child elements for deletion from the database from within the Page class?

推荐答案

经过更多的研究,我相信我已经找到了解决方案.将对象从集合中移除时将其标记为删除由 Association 属性的 DeleteOnNull 参数控制.

After some more research, I believe I've managed to find a solution. Marking an object for deletion when it's removed from a collection is controlled by the DeleteOnNull parameter of the Association attribute.

当两个表之间的关系标记为OnDelete Cascade时,该参数设置为true.

This parameter is set to true when the relationship between two tables is marked with OnDelete Cascade.

遗憾的是,无法从设计器内部设置此属性,也无法从 *DataContext.cs 文件的分部类中设置它.在不启用级联删除的情况下设置它的唯一方法是手动编辑 *DataContext.designer.cs 文件.

Unfortunately, there is no way to set this attribute from within the designer, and no way to set it from within the partial class in the *DataContext.cs file. The only way to set it without enabling cascading deletes is to manually edit the *DataContext.designer.cs file.

就我而言,这意味着找到页面关联,并添加 DeleteOnNull 属性:

In my case, this meant finding the Page association, and adding the DeleteOnNull property:

[Association(Name="Page_PageTag", Storage="_Page", ThisKey="PageId", OtherKey="iPageId", IsForeignKey=true)]
public Page Page
{
    ...
}

并添加 DeleteOnNull 属性:

And adding the DeleteOnNull attribute:

[Association(Name="Page_PageTag", Storage="_Page", ThisKey="PageId", OtherKey="iPageId", IsForeignKey=true, DeleteOnNull = true)]
public Page Page
{
    ...
}

请注意,需要将该属性添加到 PageTag 类的 Page 属性中,而不是相反.

Note that the attribute needed to be added to the Page property of the PageTag class, not the other way around.

另见:
贝丝Massi -- LINQ to SQL 和一对多关系
Dave Brace -- LINQ to SQL: DeleteOnNull

这篇关于如何从 LINQ to SQL 中的子集合中删除记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆