问题描述
我玩弄实体框架4.3,所以我现在用的的DbContext生成器来创建上下文和实体类。
I'm playing around with Entity Framework 4.3, and so I am using the DbContext Generator to create the context and entity classes.
使用默认EF 4 code生成模板,实体类实现INotifyPropertyChanged,并添加修改
和更改
的属性setter方法部分。
With the default EF 4 code generator template, entity classes implement INotifyPropertyChanged, and also add Changing
and Changed
partial methods in property setters.
当我使用EF 4.x版的DbContext发生器,如下图所示,实体类是轻得多,而且不包括跟踪属性更改的任何手段。
When I use the EF 4.x DbContext generator, as pictured below, the entity classes are much lighter, and do not include any means of tracking property changes.
下面是一个例子:
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
namespace SomeNamespace
{
public partial class SomeTable
{
public SomeTable()
{
this.Children = new HashSet<Child>();
}
public long parent_id { get; set; }
public long id { get; set; }
public string filename { get; set; }
public byte[] file_blob { get; set; }
public virtual Parent Parent { get; set; }
public virtual ICollection<Child> Children { get; set; }
}
}
我必须失去拼图的一个重要组成部分,但我的搜索已经无果而终。所以我的问题是:我怎么能已经生成的类型包括属性更改通知与EF 4.3
I must be missing an important piece of the puzzle, but my searches have been fruitless. So my question is: how can I have generated types included property change notifications with EF 4.3?
修改
我完全同意@derape答案的;但我很好奇,为什么我需要改变 .TT
文件时,EF 4默认code生成模板的已的有挂钩。我的意思是什么时候绑定到一个WPF 的DependencyProperty
?如果没有INotifyPropertyChanged的,由命令一堆特性的一组对象所作的更改将不会反映在用户界面中。我在想什么?
I fully agree with @derape answer's; but I am curious as to why I would need to change the .tt
file when the EF 4 default code generation template already has the hooks. I mean what about when binding to a WPF DependencyProperty
'? Without INotifyPropertyChanged, changes done by a command to a bunch of properties in a bunch of objects won't be reflected in the UI. What am I missing?
推荐答案
我最近偶然发现了这个问题,我编辑我的Entity.tt实现下列变化,快速更新,但伟大工程。
I recently stumbled upon this problem, i edited my Entity.tt to implement the following changes, a quick patch but works great..
添加以下到codeStringGenerator类
public string EntityClassOpening(EntityType entity)
{
return string.Format(
CultureInfo.InvariantCulture,
"{0} {1}partial class {2}{3} : {4}",
Accessibility.ForType(entity),
_code.SpaceAfter(_code.AbstractOption(entity)),
_code.Escape(entity),
_code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)),
"INotifyPropertyChanged");
}
public string Property(EdmProperty edmProperty)
{
return string.Format(
CultureInfo.InvariantCulture,
"{0} {1} {2} {{ {3}{6} {4}{5} }}",
Accessibility.ForProperty(edmProperty),
_typeMapper.GetTypeName(edmProperty.TypeUsage),
_code.Escape(edmProperty),
_code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
_code.SpaceAfter(Accessibility.ForSetter(edmProperty)),
"set { _"+_code.Escape(edmProperty).ToLower()+" = value; OnPropertyChanged(\""+_code.Escape(edmProperty)+"\");}",
"get { return _"+_code.Escape(edmProperty).ToLower()+"; }");
}
public string Private(EdmProperty edmProperty) {
return string.Format(
CultureInfo.InvariantCulture,
"{0} {1} _{2};",
"private",
_typeMapper.GetTypeName(edmProperty.TypeUsage),
_code.Escape(edmProperty).ToLower());
}
添加以下到发电机
using System.ComponentModel;
<#=codeStringGenerator.EntityClassOpening(entity)#>
{
<#
var propertiesWithDefaultValues = typeMapper.GetPropertiesWithDefaultValues(entity);
var collectionNavigationProperties = typeMapper.GetCollectionNavigationProperties(entity);
var complexProperties = typeMapper.GetComplexProperties(entity);
#>
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
而ABIT进一步回落
foreach (var edmProperty in simpleProperties)
{
#>
<#=codeStringGenerator.Private(edmProperty)#>
<#=codeStringGenerator.Property(edmProperty)#>
<#
}
foreach(var complexProperty in complexProperties)
{
#>
<#=codeStringGenerator.Private(complexProperty)#>
<#=codeStringGenerator.Property(complexProperty)#>
<#
}
这篇关于如何获得与EF 4.x版的DbContext生成属性更改通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!