2014-08-28 19 views
0

我需要爲十進制字段設置精度和刻度值。使用EF電動工具 - 檢索十進制精度和刻度

使用EF電動工具模板,我想內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

你可以做到以下幾點。

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 #>; 

<#

+0

可以因爲有在格式化明顯的錯字編輯你的答案。 – cmbarbu 2015-03-24 10:38:27