简体   繁体   English

实体框架中计算属性的数据绑定

[英]Data Binding of Computed Properties in Entity Framework

I am trying to find an effective way to databind computed fields of an Entity Framework model in winforms. 我试图找到一种有效的方法来在winforms中数据绑定实体框架模型的计算字段。 I am using Visual Studio 2008 and .NET framework 3.5. 我正在使用Visual Studio 2008和.NET framework 3.5。 An example would be for EntityX, say I have columnA and columnB, and I want a computed property named sumAB. 一个例子是EntityX,比如我有columnA和columnB,我想要一个名为sumAB的计算属性。 The way I have been doing this has been with partial classes: 我一直这样做的方式是部分课程:

public partial class EntityX 
{
    public int sumAB 
    {
        get 
        {
            return this.columnA + this.columnB;
        }
    }
}

I then am using visual studio to add EntityX as a data source, so I can use the automatic drag and drop data binding. 然后我使用visual studio将EntityX添加为数据源,因此我可以使用自动拖放数据绑定。 The issue that I am having is that the computed properties are not appearing under the Data Source fields. 我遇到的问题是计算属性没有出现在数据源字段下。 I am looking for a way to have the automatic databinding of computed fields. 我正在寻找一种方法来计算字段的自动数据绑定。

I know I could do this manually, but then I would have to also manually write all of the binding code to refresh the field when columnA or columnB are changed. 我知道我可以手动执行此操作,但是当我更改columnA或columnB时,我还必须手动编写所有绑定代码以刷新字段。 I also do not have access to make this field computed on the SQL server side either. 我也无权访问在SQL服务器端计算此字段。

Does anyone know of any ways to accomplish this or any other similar directions to persue? 有没有人知道有任何方法可以实现这个或任何其他类似的方向来说服?

Thanks! 谢谢!

UPDATE UPDATE

I tested this on another machine, using Visual Studio 2010 and .NET 4, and I am still receiving the same behavior. 我使用Visual Studio 2010和.NET 4在另一台机器上测试了这个,我仍然收到相同的行为。 Interestingly, I can manually create a textbox and add a databinding, and it will work fine, as below: 有趣的是,我可以手动创建一个文本框并添加数据绑定,它将正常工作,如下所示:

sumABTextBox.DataBindings.Add(
  new System.Windows.Forms.Binding("Text", EntityXBindingSource, "sumAB", true));

It also worked if I placed the property in the .edmx file, but this is not desirable, as it will be wiped any time the database is updated. 如果我将属性放在.edmx文件中,它也有效,但这是不可取的,因为它会在数据库更新时被擦除。

Any other ideas? 还有其他想法吗?

UPDATE 2 更新2

Still no solution... I had to use alternatives in my code to meet deadlines. 仍然没有解决方案......我不得不在我的代码中使用替代方案来满足最后期限。 I am still very interested in this if anyone finds an answer... 如果有人找到答案我还是很感兴趣...

Sounds strange to me. 听起来很奇怪。 Because I use the same approach in my project (Entity Framework, Self Tracking Entities, WCF, Winforms). 因为我在我的项目中使用相同的方法(实体框架,自我跟踪实体,WCF,Winforms)。

I constructed a simple winforms applciation to test it and the "computed" property just shows up in the databindings.Text property of a textbox control. 我构建了一个简单的winforms applciation来测试它,“computed”属性只显示在文本框控件的databindings.Text属性中。

I created a simple class: 我创建了一个简单的类:

public partial class Test
{
    public int A { get; set; }
    public int B { get; set; }

    public Test()
    {
    }
}
public partial class Test
{
    public int AB
    {
        get { return A * B; }
    }
}

Added a bindingsource to a form (with datasource pointing to an object of type Test) and three textboxes binded to the bindingsource and the properties: A, B and AB. 向表单添加了一个bindingsource(数据源指向Test类型的对象)和绑定到bindingsource的三个文本框以及属性:A,B和AB。 In the constructor of the form I created an instance of test, populated values for A and B, set bindingsource.DataSource to the created instance and thinks were groovy on the form ;) 在表单的构造函数中,我创建了一个测试实例,为A和B填充了值,将bindingsource.DataSource设置为创建的实例,并认为在表单上是groovy;)

This simple test case would be no different from your situation, other than that you work with entities which are off-course just Objects. 这个简单的测试用例与你的情况没有什么不同,除了你使用的是偏离对象的实体。 I really can't see the problem from your description...Any further comments could help me help you further... 我真的无法从你的描述中看到问题...任何进一步的评论可以帮助我进一步帮助你...

I basically just asked the same question (WPF not WinForms) yesterday - Show Computed Property in Entity Framework and WPF UI . 我昨天基本上只问了同样的问题(WPF而不是WinForms) - 在Entity Framework和WPF UI中显示计算属性

In short, I couldn't find any automatic support. 简而言之,我找不到任何自动支持。 I had to create the columns in my DataGridView for each computed property. 我必须在DataGridView中为每个计算属性创建列。 Then, to let the DataGridView know that those properties have updated, I have to call OnPropertyChanged("ComputedPropertyName") in the OnChanged partial method. 然后,为了让DataGridView知道这些属性已更新,我必须在OnChanged部分方法中调用OnPropertyChanged(“ComputedPropertyName”)。

eg 例如

public partial class Test
{
    public int AB
    {
        get { return A * B; }
    }

    public partial void OnAChanged()
    {
        OnPropertyChanged("AB");
    }
    public partial void OnBChanged()
    {
        OnPropertyChanged("AB");
    }
}

If there is an automatic way for this to work, I would like to know too. 如果有一种自动方式可以使用,我也想知道。

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

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