Binding calculated property data in the Entity Framework

I am trying to find an efficient way for calculated fields of Entity Framework model data in winforms. I am using Visual Studio 2008 and .NET framework 3.5. For example, for EntityX, let's say I have columnA and columnB, and I want a computed property called sumAB. The way I did this was with partial classes:

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

Then I use visual studio to add EntityX as a data source, so I can use automatic drag and drop data binding. The problem that I encountered is that the computed properties do not appear in the Data Source fields. I am looking for a way to automatically bind data from computed fields.

I know that I can do this manually, but then I will also have to manually write all the binding code to update the field when changing columns or columns. I also do not have access to have this field computed on the SQL server side.

Does anyone know of any ways to achieve this or any other similar areas that need to be followed?

Thank!

UPDATE

I tested this on another machine using Visual Studio 2010 and .NET 4, and I am still getting the same behavior. Interestingly, I can manually create a text box and add a data binding, and it will work fine, as shown below:

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

It also worked if I put the property in a .edmx file, but this is undesirable since it will be deleted every time the database is updated.

Any other ideas?

UPDATE 2

- ... . , - ...

+3
2

. (Entity Framework, Self Tracking Entities, WCF, Winforms).

winforms , "computed" databindings.Text .

:

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

( , Test) , : A, B AB. A B, bindingsource.DataSource , groovy ;)

, , , . ... ...

+1

(WPF WinForms) - Entity Framework WPF.

, . DataGridView . , DataGridView , , OnPropertyChanged ( "ComputedPropertyName" ) OnChanged.

.

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

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

, .

+1

Source: https://habr.com/ru/post/1779406/


All Articles