簡體   English   中英

下框由實體框架生成的查詢

[英]Lower-casing the query generated by Entity Framework

通過Code First Entity Framework執行函數時,我需要小寫列名。 我試圖在此鏈接上使用該解決方案,但它僅適用於表映射,不適用於函數映射。

這是我的POCO,用於保存函數執行中的數據。

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

這是EF生成的代碼,其中fnrbreportinfo是函數名稱, @reportId是函數參數。

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

要求是由於區分大小寫的數據庫所致,因此所有表/函數都必須是小寫的。

為什么不設置屬性名稱而不是情人大小寫命名屬性?

[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();
}

我從沒有嘗試過,但是它對您的情況很有希望,而我恰好在外面看着楓葉。 EF允許攔截,因此您可以攔截查詢,並可能在command.CommandText ToLower()上調用ToLower()

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);
    }
} 

您可以在這里閱讀更多內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM