如何使用 EF 4.x DbContext 生成器获取属性更改通知 [英] How to get property change notifications with EF 4.x DbContext generator

查看:17
本文介绍了如何使用 EF 4.x DbContext 生成器获取属性更改通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Entity Framework 4.3,因此我使用 DbContext Generator 创建上下文和实体类.

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 代码生成器模板,实体类实现 INotifyPropertyChanged,并在属性设置器中添加 ChangingChanged 部分方法.

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 的回答;但我很好奇当 EF 4 默认代码生成模板已经有钩子时,为什么我需要更改 .tt 文件.我的意思是绑定到 WPF DependencyProperty' 时怎么样?如果没有 INotifyPropertyChanged,由命令对一组对象中的一组属性所做的更改将不会反映在 UI 中.我错过了什么?

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

再往下走

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 生成器获取属性更改通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆