向Entity Framework 4添加代码生成了POCO [英] Adding code to Entity Framework 4 generated POCOs

查看:175
本文介绍了向Entity Framework 4添加代码生成了POCO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从EF 4实体图开始,使用T4模板可以创建可在域模型中使用的POCO类。生成的代码如下所示:

Starting from an EF 4 entity diagram and using T4 templates one can create POCO classes that can be used inside the Domain Model. The generated code looks like this:

public partial class Product
{
    public virtual int Id
    {
        get;
        set;
    }

    public virtual string Name
    {
        get;
        set;
    }
   //and so on
}

是否任何优雅的方法来添加我自己的代码来实现属性?例如,我想通过降低所有字符来实现Name设置器。我希望我的代码可以反映从EF图中重复重新生成POCO类。

Is there any elegant approach to add my own code for implementing the properties? for example, the Name setter I would like to be implemented by lowering all the characters. I would like that my code resist to repeated regeneration of the POCO classes from the EF diagram.

这个要求有点类似于向POCO类添加验证码。通过创建单独的验证类并通过 MetadataType 属性将其链接到POCO,已经解决了此问题。

This requirement is somewhat similar to adding validation code to the POCO classes. This issue is already solved by creating a separate validation class and linking it to the POCO through the MetadataType attribute. That separate validation class is not overwritten by repeatedly regenerating POCOs from the EF diagram.

谢谢,

Lucian

推荐答案

没有没有简单的方法来做到这一点。您不能触摸生成的代码,因为您的更改将在每次重新生成后被删除。您的选项是:

No there is no easy way to do that. You must not touch generated code because your changes will be deleted after each regeneration. You options are:


  • 自己编写实体,不要使用生成器 - 您将完全控制实体代码

  • 在生成器中修改T4模板以添加直接代码(这可能很难推广),或者简单地添加对部分方法的调用(您还必须在生成器中声明这些方法)在getter和setter以及部分方法中实体类的一部分实现了您需要的部分方法。

这篇关于向Entity Framework 4添加代码生成了POCO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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