StoreGeneratePattern T4 EntityFramework 关注点
情况如下:
我使用 SQL Server 2008 R2、SQL 复制、Visual Studio 2010、EntityFramework 4、C# 4。
我们的 DBA 的操作过程是使用 SQL 复制的 rowguid 列来配合我们的设置。这些列需要在每一列上将 StoreGeneratePattern 属性设置为 Computed。
问题:
每次 T4 模板重新生成我们的 EDMX(ADO.NET 实体数据模型)文件时(例如,当我们从数据库更新它时),我需要手动在 EDMX XML 文件中将此属性添加到每个文件中其中。它必须从这个:
<Property Name="rowguid" Type="uniqueidentifier" Nullable="false" />
到这个:
<Property Name="rowguid" Type="uniqueidentifier" Nullable="false" StoreGeneratedPattern="Computed"/>
解决方案:
我正在尝试找到一种方法来自定义 ADO.NET EntityObject Generator T4 文件,以便为我拥有的每个 rowguid 生成 StoreGeneratePattern="Compulated"。
我对 T4 相当陌生,我只对 ASP.NET MVC 2 的 AddView 和 AddController T4 模板进行了自定义,例如 List.tt。
我已经浏览了 EF T4 文件,但我似乎无法通过这个怪物找到我可以在哪里做到这一点(以及如何做到)。
我最好的猜测是在文件的这一部分中的某个位置,即原始 ADO.NET EntityObject Generator T4 文件的第 544 行到 618 行:
////////
//////// Write PrimitiveType Properties.
////////
private void WritePrimitiveTypeProperty(EdmProperty primitiveProperty, CodeGenerationTools code)
{
MetadataTools ef = new MetadataTools(this);
#>
/// <summary>
/// <#=SummaryComment(primitiveProperty)#>
/// </summary><#=LongDescriptionCommentElement(primitiveProperty, 1)#>
[EdmScalarPropertyAttribute(EntityKeyProperty=<#=code.CreateLiteral(ef.IsKey(primitiveProperty))#>, IsNullable=<#=code.CreateLiteral(ef.IsNullable(primitiveProperty))#>)]
[DataMemberAttribute()]
<#=code.SpaceAfter(NewModifier(primitiveProperty))#><#=Accessibility.ForProperty(primitiveProperty)#> <#=code.Escape(primitiveProperty.TypeUsage)#> <#=code.Escape(primitiveProperty)#>
{
<#=code.SpaceAfter(Accessibility.ForGetter(primitiveProperty))#>get
{
<#+ if (ef.ClrType(primitiveProperty.TypeUsage) == typeof(byte[]))
{
#>
return StructuralObject.GetValidValue(<#=code.FieldName(primitiveProperty)#>);
<#+
}
else
{
#>
return <#=code.FieldName(primitiveProperty)#>;
<#+
}
#>
}
<#=code.SpaceAfter(Accessibility.ForSetter((primitiveProperty)))#>set
{
<#+
if (ef.IsKey(primitiveProperty))
{
if (ef.ClrType(primitiveProperty.TypeUsage) == typeof(byte[]))
{
#>
if (!StructuralObject.BinaryEquals(<#=code.FieldName(primitiveProperty)#>, value))
<#+
}
else
{
#>
if (<#=code.FieldName(primitiveProperty)#> != value)
<#+
}
#>
{
<#+
PushIndent(CodeRegion.GetIndent(1));
}
#>
<#=ChangingMethodName(primitiveProperty)#>(value);
ReportPropertyChanging("<#=primitiveProperty.Name#>");
<#=code.FieldName(primitiveProperty)#> = StructuralObject.SetValidValue(value<#=OptionalNullableParameterForSetValidValue(primitiveProperty, code)#>);
ReportPropertyChanged("<#=primitiveProperty.Name#>");
<#=ChangedMethodName(primitiveProperty)#>();
<#+
if (ef.IsKey(primitiveProperty))
{
PopIndent();
#>
}
<#+
}
#>
}
}
private <#=code.Escape(primitiveProperty.TypeUsage)#> <#=code.FieldName(primitiveProperty)#><#=code.StringBefore(" = ", code.CreateLiteral(primitiveProperty.DefaultValue))#>;
partial void <#=ChangingMethodName(primitiveProperty)#>(<#=code.Escape(primitiveProperty.TypeUsage)#> value);
partial void <#=ChangedMethodName(primitiveProperty)#>();
<#+
}
任何帮助将不胜感激。提前致谢。
编辑:还没有找到这个问题的答案,如果有人有自动执行此操作的想法,我们将不胜感激。
Here's the situation :
I use SQL Server 2008 R2, SQL Replication, Visual Studio 2010, EntityFramework 4, C# 4.
The course-of-action from our DBA is to use a rowguid column for SQL Replication to work with our setup. These columns need to have a StoreGeneratedPattern property set to Computed on every one of these columns.
The problem :
Every time the T4 template regenerate our EDMX (ADO.NET Entity Data Model) file (for example, when we update it from our database), I need to go manually in the EDMX XML file to add this property to every one of them. It has to go from this :
<Property Name="rowguid" Type="uniqueidentifier" Nullable="false" />
To this :
<Property Name="rowguid" Type="uniqueidentifier" Nullable="false" StoreGeneratedPattern="Computed"/>
The solution :
I'm trying to find a way to customize an ADO.NET EntityObject Generator T4 file to generate a StoreGeneratedPattern="Computed" to every rowguid that I have.
I'm fairly new to T4, I only did customization to AddView and AddController T4 templates for ASP.NET MVC 2, like List.tt for example.
I've looked through the EF T4 file, and I can't seem to find through this monster where I could do that (and how).
My best guess is somewhere in this part of the file, line 544 to 618 of the original ADO.NET EntityObject Generator T4 file :
////////
//////// Write PrimitiveType Properties.
////////
private void WritePrimitiveTypeProperty(EdmProperty primitiveProperty, CodeGenerationTools code)
{
MetadataTools ef = new MetadataTools(this);
#>
/// <summary>
/// <#=SummaryComment(primitiveProperty)#>
/// </summary><#=LongDescriptionCommentElement(primitiveProperty, 1)#>
[EdmScalarPropertyAttribute(EntityKeyProperty=<#=code.CreateLiteral(ef.IsKey(primitiveProperty))#>, IsNullable=<#=code.CreateLiteral(ef.IsNullable(primitiveProperty))#>)]
[DataMemberAttribute()]
<#=code.SpaceAfter(NewModifier(primitiveProperty))#><#=Accessibility.ForProperty(primitiveProperty)#> <#=code.Escape(primitiveProperty.TypeUsage)#> <#=code.Escape(primitiveProperty)#>
{
<#=code.SpaceAfter(Accessibility.ForGetter(primitiveProperty))#>get
{
<#+ if (ef.ClrType(primitiveProperty.TypeUsage) == typeof(byte[]))
{
#>
return StructuralObject.GetValidValue(<#=code.FieldName(primitiveProperty)#>);
<#+
}
else
{
#>
return <#=code.FieldName(primitiveProperty)#>;
<#+
}
#>
}
<#=code.SpaceAfter(Accessibility.ForSetter((primitiveProperty)))#>set
{
<#+
if (ef.IsKey(primitiveProperty))
{
if (ef.ClrType(primitiveProperty.TypeUsage) == typeof(byte[]))
{
#>
if (!StructuralObject.BinaryEquals(<#=code.FieldName(primitiveProperty)#>, value))
<#+
}
else
{
#>
if (<#=code.FieldName(primitiveProperty)#> != value)
<#+
}
#>
{
<#+
PushIndent(CodeRegion.GetIndent(1));
}
#>
<#=ChangingMethodName(primitiveProperty)#>(value);
ReportPropertyChanging("<#=primitiveProperty.Name#>");
<#=code.FieldName(primitiveProperty)#> = StructuralObject.SetValidValue(value<#=OptionalNullableParameterForSetValidValue(primitiveProperty, code)#>);
ReportPropertyChanged("<#=primitiveProperty.Name#>");
<#=ChangedMethodName(primitiveProperty)#>();
<#+
if (ef.IsKey(primitiveProperty))
{
PopIndent();
#>
}
<#+
}
#>
}
}
private <#=code.Escape(primitiveProperty.TypeUsage)#> <#=code.FieldName(primitiveProperty)#><#=code.StringBefore(" = ", code.CreateLiteral(primitiveProperty.DefaultValue))#>;
partial void <#=ChangingMethodName(primitiveProperty)#>(<#=code.Escape(primitiveProperty.TypeUsage)#> value);
partial void <#=ChangedMethodName(primitiveProperty)#>();
<#+
}
Any help would be appreciated. Thanks in advance.
EDIT : Didn't find answer to this problem yet, if anyone have ideas to automate this, would really be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该模板负责 C# 代码生成。它不会影响模型XML的生成。
尝试就存储再生问题联系 Microsoft。
This template is responsible for the C# code generation. It will not affect the model XML generation.
Try contacting Microsoft on the Storage regeneration issue.