使用EF Power Tools - 检索十进制精度和比例

时间:2014-08-28 19:35:42

标签: entity-framework t4

我需要设置十进制字段的精度和比例值。

使用EF Powertools模板,我想在Mapping.tt中自定义此部分:

foreach (var prop in efHost.EntityType.Properties)
{
    var type = (PrimitiveType)prop.TypeUsage.EdmType;
    var isKey = efHost.EntityType.KeyMembers.Contains(prop);
    var storeProp = efHost.PropertyToColumnMappings[prop];
    var sgpFacet = storeProp.TypeUsage.Facets.SingleOrDefault(f => f.Name == "StoreGeneratedPattern");
    var storeGeneratedPattern = sgpFacet == null
        ? StoreGeneratedPattern.None
        : (StoreGeneratedPattern)sgpFacet.Value;

    // Other code from Mapping.tt

    if (type.ClrEquivalentType == typeof(decimal))
    {
        int precision = **[Retrieve Precision Value]**;
        int scale =  **[Retrieve Scale Value]**;

        configLines.Add(String.Format(".HasPrecision({0}, {1})", precision, scale));
     }
}

如何检索每个精度比例值?

感谢。

史蒂夫

1 个答案:

答案 0 :(得分:1)

你可以按照以下方式进行。

    if (edmType.ClrEquivalentType == typeof(decimal))
    {            
        Facet precisionFacet=  property.TypeUsage.Facets.SingleOrDefault(f => f.Name == "Precision");    
        Facet scaleFacet =  property.TypeUsage.Facets.SingleOrDefault(f => f.Name == "Scale");                

        hasPrecision=String.Format(".HasPrecision({0}, {1})", precisionFacet.Value, scaleFacet.Value);

     }
    if(!entityPropertyToColumnMapping.Item2.Keys.Contains(property)) continue;
    string hasColumnName = string.Format(".HasColumnName(\"{0}\")", entityPropertyToColumnMapping.Item2[property].Name);
    if(property.Name=="AccountID"){blnHasAccountId=true;}

>

      this.Property(t => t.<#= property.Name#>)<#=hasColumnName+ hasPrecision + generateOption + required + unicCode + fixedLength + maxLength #>;

&lt;#