如何在实体框架实体上实现INotifyPropertyChanged [英] How to Implement INotifyPropertyChanged on Entity Framework Entity
问题描述
它有一个属性AccountTypeId。
当它更改时,我需要一个通知,因此可以更新其他字段。
这样做通常如何?这只需要一个属性,所以我不想使用修改的模板。
AccountTypeId绑定到WinForms中的用户界面中的ComboBox,所以它不是真的是一个MVVM应用程序,我通常会在WPF中。
一种方法是去EDMX并重命名该字段AccountTypeID_Internal(例如),并且在EDMX中将属性设置为 Private
。然后创建一个Partial Class。
生成的Account.cs应如下所示:
// --------------------------------------------- ---------------------------------
//<自动生成>
//该代码是从模板生成的。
//
//手动更改此文件可能会导致应用程序出现意外的行为。
//如果重新生成代码,则对该文件的手动更改将被覆盖。
//< / auto-generated>
// -------------------------------------------- ----------------------------------
命名空间< yournamespace>
{
using System;
使用System.Collections.Generic;
public partial class Account
{
private int AccountTypeId_Internal {get;组; }
//其他自动生成的属性
}
}
Account.Partial.Cs
public partial class Account:INotifyPropertyChanged
{
public Int AccountTypeId
{
get
{
return this.AccountTypeId_Internal;
}
set
{
this.AccountTypeId_Internal = value;
//做INotifyPropertyChangedLogic
}
}
//实现INotifyPropertyChanged
}
这样做的优点是您已经编写的代码完全不需要更改。缺点是如果从edmx中删除Accout并重新添加它,则必须再次执行edmx步骤。
I have an EF 5 entity called Account generated with the standard template.
It has a property AccountTypeId.
When it changes, I need a notification so another field can be updated.
How is this typically done? This is needed for only one property, so I don't want to use a modified template.
AccountTypeId is bound to a ComboBox in the UI in WinForms, so it's not really an MVVM app as I would normally do in WPF.
One way is to go to the EDMX and rename the field to AccountTypeID_Internal (for example), and in the EDMX set the property to Private
. Then create a Partial Class.
Generated Account.cs should look like:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace <yournamespace>
{
using System;
using System.Collections.Generic;
public partial class Account
{
private int AccountTypeId_Internal { get; set; }
// other auto generated properties
}
}
Account.Partial.Cs
public partial class Account : INotifyPropertyChanged
{
public Int AccountTypeId
{
get
{
return this.AccountTypeId_Internal;
}
set
{
this.AccountTypeId_Internal = value;
// Do INotifyPropertyChangedLogic
}
}
// Implement INotifyPropertyChanged
}
The advantage of this is that your already written code doesn't have to change at all. The downside is that if you remove the Accout from the edmx and re-add it you will have to go through the edmx steps again.
这篇关于如何在实体框架实体上实现INotifyPropertyChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!