C# 实体框架生成的查询的下框
我需要在通过代码优先实体框架执行函数时降低列名的大小写。我曾尝试在使用该解决方案,但它仅适用于表映射,而不适用于函数映射 这是我的POCO,它保存函数执行过程中的数据C# 实体框架生成的查询的下框,c#,sql,entity-framework,properties,ef-code-first,C#,Sql,Entity Framework,Properties,Ef Code First,我需要在通过代码优先实体框架执行函数时降低列名的大小写。我曾尝试在使用该解决方案,但它仅适用于表映射,而不适用于函数映射 这是我的POCO,它保存函数执行过程中的数据 public class RBReportInfo { [Key] public int ReportId { get; set; } public int ReportDataViewId { get; set; } } 这是EF生成的代码,fnrbreportinfo是函数名,@repor
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);
因此,我希望EF生成的查询如下,其中列名是小写的
SELECT
[Extent1].[reportid] AS [ReportId],
[Extent1].[reportdataviewid] AS [ReportDataViewId]
FROM [dbo].[fnrbreportinfo](@reportId) AS [Extent1]
我曾尝试将属性名称改为小写,但它打破了C#中属性命名的PascalCase约定
我也尝试过使用属性,但也失败了
[Column("reportid")]
public int ReportId{get;set;}
这是因为需要区分大小写的数据库,因此所有的表/函数都是小写的。为什么不设置属性名而不是大小写命名属性
SELECT
[Extent1].[reportid] AS [ReportId],
[Extent1].[reportdataviewid] AS [ReportDataViewId]
FROM [dbo].[fnrbreportinfo](@reportId) AS [Extent1]
[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());
}
我从来没有尝试过这个,但它看起来对你的情况很有希望,我现在正好在外面看枫叶。EF允许拦截,因此您可以拦截查询,并可能在
命令上调用ToLower()
。CommandText
:
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);
}
}
类EFCommandInterceptor:IDBComandInterceptor
{
public void NonQueryExecuted(System.Data.Common.DbCommand命令,DbCommandInterceptionContext interceptionContext)
{
LogInfo(“NonQueryExecuted”,String.Format(“IsAsync:{0},命令文本:{1}”,interceptonContext.IsAsync,Command.CommandText));
}
public void非查询执行(System.Data.Common.DbCommand命令,DbCommandInterceptionContext interceptionContext)
{
LogInfo(“NonQueryExecuting”,String.Format(“IsAsync:{0},命令文本:{1}”,interceptonContext.IsAsync,Command.CommandText));
}
public void ReaderExecuted(System.Data.Common.DbCommand命令,DbCommandInterceptionContextt interceptionContext)
{
LogInfo(“ReaderExecuted”,String.Format(“IsAsync:{0},命令文本:{1}”,interceptonContext.IsAsync,Command.CommandText));
}
public void reader执行(System.Data.Common.DbCommand命令,DbCommandInterceptionContext interceptionContext)
{
LogInfo(“ReaderExecuting”,String.Format(“IsAsync:{0},命令文本:{1}”,interceptionContext.IsAsync,Command.CommandText));
}
已执行公共无效刻度(System.Data.Common.DbCommand命令,DBCommandInterceptOnContext InterceptOnContext)
{
LogInfo(“ScalarExecuted”,String.Format(“IsAsync:{0},命令文本:{1}”,interceptonContext.IsAsync,Command.CommandText));
}
public void scalare执行(System.Data.Common.DbCommand命令,DbCommandInterceptionContext interceptionContext)
{
LogInfo(“ScalarExecuting”,String.Format(“IsAsync:{0},命令文本:{1}”,interceptionContext.IsAsync,Command.CommandText));
}
私有void LogInfo(字符串命令、字符串命令文本)
{
WriteLine(“截获时间:{0}:-{1}”,命令,commandText);
}
}
你可以读更多 “这是因为需要区分大小写的数据库,因此所有表/函数都将以小写形式显示。”建议您不要使用SQL Server。请说明您正在使用的数据库引擎和实体框架提供程序。听起来EF提供商(非微软)有一个bug
using(var context = new DBContext())
{
context.SomeTable.SelectFromDB(data => data).ToList();
}
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);
}
}