StoreGeneratedPattern T4 EntityFramework concern

Posted by LoganWolfer on Stack Overflow See other posts from Stack Overflow or by LoganWolfer
Published on 2010-12-16T16:05:57Z Indexed on 2011/01/07 18:53 UTC
Read the original article Hit count: 188

Hi everyone,

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.

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about c#-4.0