使用T4自定义一些自动生成的代码

问题描述:

我首先使用"EF 4.x DbContext Fluent Genarator"生成了我的poco类,映射文件以及上下文文件.(实际上,"EF 4.x DbContext Fluent Genarator"使用了3个T4文件)现在我想对自动生成的代码进行一些更改:

I Used "EF 4.x DbContext Fluent Genarator" to generate my poco classes , mapping files and also Context file, in EF Code first.(in fact "EF 4.x DbContext Fluent Genarator" uses 3 T4 files) now i want apply some changes on auto generated codes:

  1. 更改某些类的名称空间.
  2. 将某些类的某些字段标记为[NonSerializable]
  3. 更改我添加到T4模板的pocos上一些自动生成的自定义方法的主体.

如何继续使用T4文件来完成这些工作?

how can i do these works with continuing using T4 files?

适用于#3.这应该有帮助

for #3. this should help

这些类是部分类,因此您可以使用与部分相同的类来创建一个新的.cs文件.添加自定义方法.

These classes are partial classes so you can create a new .cs file with same class as partial. add custom methods.

我不认为#1的直接方法.&#.2大小写当有人再次更新/构建它时,对自动生成的类所做的更改将丢失.

I can not think a straight forward way for #1. & #.2 becase Changes to Auto generated classes will be lost when someone updates / build it again.