2017-04-18 15 views
0

コードファーストエンティティフレームワークを介して関数を実行する際に、カラム名を小文字にする必要があります。私はthis linkで解決策を使用しようとしましたが、テーブルマッピングと関数マッピングでは機能しません。Entity Frameworkによって生成されたクエリの下部ケーシング

これは、関数の実行からデータを保持する私のPOCOです。

public class RBReportInfo 
{ 
    [Key] 
    public int ReportId { get; set; } 
    public int ReportDataViewId { get; set; } 
} 

これはfnrbreportinfo関数名であり、@reportId関数パラメータであるEFによって生成されたコードです。

SELECT 
[Extent1].[ReportId] AS [ReportId], 
[Extent1].[ReportDataViewId] AS [ReportDataViewId] 
FROM [dbo].[fnrbreportinfo](@reportId) AS [Extent1] 

以下のコードは、POCOに入力するために実行されます。

var idParameter = new ObjectParameter("reportId", reportId); 
return ((IObjectContextAdapter)this).ObjectContext.CreateQuery<RBReportInfo>(
      $"[{nameof(ReportBuilderContext)}].[fnrbreportinfo](@reportId)", idParameter); 

このコードは、クエリで小文字データベースの列名と一致する特性の場合を変更OnModelCreating()方法で実装されています。

 modelBuilder.Properties().Configure(c => 
     { 
      var name = c.ClrPropertyInfo.Name.ToLower(); 
      c.HasColumnName(name); 
     }); 

したがって、EFで生成されたクエリは、列名が小文字の次のようになります。

SELECT 
[Extent1].[reportid] AS [ReportId], 
[Extent1].[reportdataviewid] AS [ReportDataViewId] 
FROM [dbo].[fnrbreportinfo](@reportId) AS [Extent1] 

私は動作しますが、それはC#でプロパティの命名のためのPascalCase規則を破る - 小文字にプロパティ名を変更しようとしています。

public class RBReportInfo 
{ 
    [Key] 
    public int reportid { get; set; } 
    public int reportdataviewid { get; set; } 
} 

私も属性を使用しようとしましたが、失敗しました。

[Column("reportid")] 
public int ReportId{get;set;} 

この要件は、すべてのテーブル/機能が小文字になるため、大文字と小文字が区別されるデータベースのためです。

+1

"テーブル/機能がすべて小文字になるため、大文字と小文字を区別するデータベースが必要なためです。" SQL Serverを使用していないことを示します。使用しているデータベースエンジンとEntity Framework Providerを明記してください。それはEFプロバイダー(非マイクロソフト)のバグがあるように聞こえます – Aron

答えて

0

などのカスタムメソッドを追加することができます今ではメープルが葉っぱを見るのを見て外に出てしまいます。

class EFCommandInterceptor: IDbCommandInterceptor 
{ 
    public void NonQueryExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext) 
    { 
     LogInfo("NonQueryExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText)); 
    } 

    public void NonQueryExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext) 
    { 
     LogInfo("NonQueryExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText)); 
    } 

    public void ReaderExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContextt<System.Data.Common.DbDataReader> interceptionContext) 
    { 
     LogInfo("ReaderExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText)); 
    } 

    public void ReaderExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext) 
    { 
     LogInfo("ReaderExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText)); 
    } 

    public void ScalarExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext) 
    { 
     LogInfo("ScalarExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText)); 
    } 

    public void ScalarExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext) 
    { 
     LogInfo("ScalarExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText)); 
    } 

    private void LogInfo(string command, string commandText) 
    { 
     Console.WriteLine("Intercepted on: {0} :- {1} ", command, commandText); 
    } 
} 

あなたはhere詳細を読むことができます:あなたは、クエリを傍受し、おそらくcommand.CommandTextToLower()を呼び出すことができますので、EFは、傍受することができます。

0

恋人の名前付けプロパティの代わりに属性名を設定しないのはなぜですか?

[Table("mytable")] 
public class MyTable { 

    [Column("firstcolumn")] 
    public int firstColumn {get;set} 

    [Column("secondcolumn")] 
    public string secondColumn {get;set;} 

} 

それは解決策ではないのですが、あなたは、私はこれを試したことがないが、それはあなたの場合、私のために有望に見えるこの

public IQueriable<TResult> SelectFromDB<TSource, TResult>(this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector) 
{ 
     return source.SqlQuery(source.Select(selector).ToString().ToLower()); 
} 

そして

using(var context = new DBContext()) 
{ 
    context.SomeTable.SelectFromDB(data => data).ToList(); 
} 
+0

私はそれが動作しなかったことに言及するのを忘れました。 –

関連する問題