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

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

问题描述

我的数据库中有两个通过外键连接的表:Page(PageId,其他数据)和PageTag(PageId,Tag).我使用LINQ为这些表生成类,以页面为父级,将Tag作为子级集合(一对多关系).有什么方法可以将PageTag记录标记为要从Page类内部从数据库中删除吗?

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对象中添加了一个属性,该属性将Tag集合视为字符串:

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级联标记时,此参数设置为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.

在我的情况下,这意味着找到Page关联,并添加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.

另请参阅:
Beth Massi-LINQ to SQL和一对多关系
戴夫·布雷斯(Dave Brace)– LINQ to SQL :DeleteOnNull

See also:
Beth Massi -- LINQ to SQL and One-To-Many Relationships
Dave Brace -- LINQ to SQL: DeleteOnNull

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

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