2016-06-14 67 views
0

我有大約50個數據庫,每個數據庫有150個表,並且使用搜索機制來查詢具有特定列的表。大多數數據庫結構是相似的,所以想法是生成EF實體並將接口放置在生成的實體後面,如果生成的表具有特定的列,以便稍後可以在該列上查詢。將接口放在具有相同名稱但屬性不同的屬性後面

After doing this我遇到了一些表列,我試圖映射意想不到的問題是Long,而別人它的Decimal(甲骨文),有時它Nullable,有時不是。

我可以看到的一個解決方案是在運行查詢之前添加沒有任何屬性的接口,並在運行時確定該列的特定類型。

有沒有辦法在接口上添加一些類型的屬性,可能會超過一種類型,所以編譯器不會抱怨沒有實現?對象不起作用,讓我知道你是否需要代碼示例,因爲問題有點複雜。

機制,我打算在實體上使用:

public static class ExpressionExtensions 
{ 
    public static IQueryable<T> ApplySearch<T>(this IQueryable<T> queryable, SearchModel search) where T : class 
    { 
     if (search != null && search.PolicyNumber.HasValue && typeof(IPolicyNumber).IsAssignableFrom(queryable.ElementType)) 
     { 
      queryable = queryable.SearchByPolicyNumber(search); 
     } 

     return queryable; 
    } 

    public static IQueryable<IPolicyNumber> SearchByPolicyNumber<IPolicyNumber>(this IQueryable<IPolicyNumber> queryable, SearchModel search) 
    { 
     var policyNumberParameterLambda = Expression.Parameter((typeof(IPolicyNumber))); 
     var policyNumberColumnLambda = Expression.Property(policyNumberParameterLambda, "POLICY_NO"); 
     var lambda = Expression.Lambda<Func<IPolicyNumber, bool>>(
      Expression.Equal(policyNumberColumnLambda, 
       Expression.Convert(Expression.Constant(search.PolicyNumber), policyNumberColumnLambda.Type) 
     ), policyNumberParameterLambda); 
     return queryable.Where(lambda); 
    } 
} 
+0

泛型FTW ... – spender

+0

@spender泛型FTW同志... :) –

回答

0

想通了自己

接口是被放在實體必須從這個

public interface IUniqueId 
{ 
    long UNIQUE_ID { get; set; } 
} 

改變這個

public interface IUniqueId<T> : IUniqueId 
{ 
    T UNIQUE_ID { get; set; } 
} 

public interface IUniqueId 
{ 
} 

,然後其中接口被放置在實體

public partial class QUOTE_HOUSE : IUniqueId 
{ 
    public long UNIQUE_ID { get; set; } 
... 
} 

public partial class QUOTE_HOUSE : IUniqueId<long> 
{ 
    public long UNIQUE_ID { get; set; } 
.... 
} 

T4代碼that generates interfaces

public string EntityClassOpening(EntityType entity) 
{ 
    var stringsToMatch = new Dictionary<string,string> { { "POLICY_NO", "IPolicyNumber" }, { "UNIQUE_ID", "IUniqueId" } }; 
    return string.Format(
     CultureInfo.InvariantCulture, 
     "{0} {1}partial class {2}{3}{4}", 
     Accessibility.ForType(entity), 
     _code.SpaceAfter(_code.AbstractOption(entity)), 
     _code.Escape(entity), 
     _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)), 
     stringsToMatch.Any(o => entity.Properties.Any(n => n.Name == o.Key)) ? " : " + string.Join(", ", stringsToMatch.Join(entity.Properties, l => l.Key, r => r.Name, (l,r) => l.Value)) : string.Empty); 
} 
改變

public string EntityClassOpening(EntityType entity) 
{ 
    var stringsToMatch = new Dictionary<string,string> { { "POLICY_NO", "IPolicyNumber" }, { "UNIQUE_ID", "IUniqueId" } }; 
    return string.Format(
     CultureInfo.InvariantCulture, 
     "{0} {1}partial class {2}{3}{4}", 
     Accessibility.ForType(entity), 
     _code.SpaceAfter(_code.AbstractOption(entity)), 
     _code.Escape(entity), 
     _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)), 
     stringsToMatch.Any(o => entity.Properties.Any(n => n.Name == o.Key)) ? " : " + string.Join(", ", stringsToMatch.Join(entity.Properties, l => l.Key, r => r.Name, (l,r) => string.Format("{0}<{1}{2}>", l.Value, r.TypeUsage.EdmType.Name, r.Nullable && r.TypeUsage.EdmType.Name!="String" ? "?" : ""))) : string.Empty); 
} 

泛型FTW確實...

希望這可以節省一些時間。

相關問題