如何将“IsDirty”属性添加到LINQ to SQL实体?

时间:2022-06-24 21:09:26

I am binding my entities to an edit form in WPF. Within a DataTemplate, I want to be able to set the background color of the root container within a DataTemplate to show it has been changed and these changes have not yet been submitted to the database.

我将我的实体绑定到WPF中的编辑表单。在DataTemplate中,我希望能够在DataTemplate中设置根容器的背景颜色,以显示它已被更改,并且这些更改尚未提交到数据库。

Here's a very simple sample that demonstrates what I'm talking about (forgive errors):

这是一个非常简单的示例,演示了我正在谈论的内容(原谅错误):

<Page ...>
    <Page.DataContext>
        <vm:MyPageViewModel /> <!-- Holds reference to the DataContext -->
    </Page.DataContext>
    <ItemsControl
        ItemsSource = {Binding Items}>
        <ItemsControl.Resources>
            <DataTemplate
                DataType="Lol.Models.Item"> <!-- Item is L2S entity -->
                <!-- In real life, I use styles to set the background color -->
                <TextBlock Text="{Binding IsDirty, StringFormat='Am I dirty? /{0/}'}"/>
            </DataTemplate>
        </ItemsControl.Resources>
    </ItemsControl>
</Page>

The example just prints out "Am I dirty? yes" or "Am I dirty? no", but you get the idea.

这个例子只打印出“我是肮脏的吗?是的”还是“我是不是很脏?没有”,但你明白了。

To do this, I'll need to add a public property to my Item (partial class, simple) that can determine if the entity is dirty or not. This is the tough bit.

为此,我需要向我的Item(partial class,simple)添加一个公共属性,以确定实体是否脏。这是艰难的一点。

public partial class Item
{
    public bool IsDirty
    {
        get
        {
            throw new NotImplementedException("hurf durf");
        }
    }
}

Outside of the entity, it's pretty simple (as long as you have the DataContext the entity is attached to). Inside, not so much.

在实体之外,它非常简单(只要你有附加实体的DataContext)。里面,没有那么多。

What are my options here?

我有什么选择?


Edit: I don't think there's one good solution here, so suggestions for workarounds are welcome.

编辑:我认为这里没有一个好的解决方案,因此欢迎提供变通方法的建议。

(Okay, similar questions exist, but they are all about how to determine this from outside of the entity itself and use the DataContext the entity is attached to.)

(好的,存在类似的问题,但它们都是关于如何从实体本身外部确定这个问题并使用实体所附加的DataContext。)

1 个解决方案

#1


If you are using the dbml generated classes, you should be able to implement a couple of partial methods like this:

如果您正在使用dbml生成的类,那么您应该能够实现几个部分方法,如下所示:

public partial class SampleEntity
{
    partial void OnCreated()
    {
        this.IsDirty = true;
    }

    partial void OnLoaded()
    {
        this.PropertyChanged += (s, e) => this.IsDirty = true;
        this.IsDirty = false;
    }

    public bool IsDirty { get; private set; }
}

#1


If you are using the dbml generated classes, you should be able to implement a couple of partial methods like this:

如果您正在使用dbml生成的类,那么您应该能够实现几个部分方法,如下所示:

public partial class SampleEntity
{
    partial void OnCreated()
    {
        this.IsDirty = true;
    }

    partial void OnLoaded()
    {
        this.PropertyChanged += (s, e) => this.IsDirty = true;
        this.IsDirty = false;
    }

    public bool IsDirty { get; private set; }
}