Raj*_*dam 5 c# entity-framework-core ef-core-3.0
我正在将应用程序迁移到 .net core。但 SqlFunctions.StringConvert() 在 .netcore 中不起作用。我正在使用以下代码:
using (var entity = new AppEntity())
{
var physianAggregationList = (from physianAggregaton in entity.tbl_Physician_Aggregation_Year
where (
(physianAggregaton.CMS_Submission_Year == cmsyear)
&& (physianAggregaton.Physician_NPI == npi || (string.IsNullOrEmpty(npi)))
&& physianAggregaton.Is_90Days == is_90Days && physianAggregaton.Exam_TIN == Tin
)
select new Tindata
{
Performance_Rate = entity.tbl_Lookup_Measure.Where(i => i.Measure_num.ToLower() == physianAggregaton.Measure_Num.ToLower()
&& i.CMSYear == physianAggregaton.CMS_Submission_Year)
.Select(x => x.Perf_Rate_Per_1000).FirstOrDefault() == boolValue
? (physianAggregaton.Performance_rate == null ? "NULL" : SqlFunctions.StringConvert(physianAggregaton.Performance_rate, 100, 2))
: (physianAggregaton.Performance_rate == null ? "NULL" : SqlFunctions.StringConvert(physianAggregaton.Performance_rate, 100, 2) + "%"),
Reporting_Rate = physianAggregaton.Reporting_Rate == null ? "NULL" : SqlFunctions.StringConvert(
//}).Distinct().OrderBy(x=>x.displayorder).ToList();
}).Distinct().OrderBy(x => x.LastName).ToList();
///logger.LogInfo("PhysianAggregation Count in getPhysianAggregationData():[" + physianAggregationList.Count()+"]");
}
Run Code Online (Sandbox Code Playgroud)
SqlFunctions是 EF6 特定类,不能在 EF Core 中使用。EF Core 自定义函数可作为EF.Functions.
不幸的是,目前 EF Core 不提供等效的StringConvert. 但通过使用 EF Core数据库标量函数映射以及映射到STR与 EF6 所做的类似的方法,可以相对轻松地添加它。
例如,添加以下类(带有必要的usings):
public static class SqlFunctions
{
public static string ToString(this decimal? value, int? length, int? decimalArg) => throw new NotSupportedException();
public static string ToString(this double? value, int? length, int? decimalArg) => throw new NotSupportedException();
public static ModelBuilder AddSqlFunctions(this ModelBuilder modelBuilder) => modelBuilder
.MapToSTR(() => ToString(default(decimal?), null, null))
.MapToSTR(() => ToString(default(double?), null, null));
static ModelBuilder MapToSTR(this ModelBuilder modelBuilder, Expression<Func<string>> method)
{
modelBuilder.HasDbFunction(method).HasTranslation(args =>
new SqlFunctionExpression(null, null, "STR", false, args, true, typeof(string), null));
return modelBuilder;
}
}
Run Code Online (Sandbox Code Playgroud)
然后在您的覆盖中添加以下内容OnModelCreating:
if (Database.IsSqlServer()) modelBuilder.AddSqlFunctions();
Run Code Online (Sandbox Code Playgroud)
然后在查询中替换
SqlFunctions.StringConvert(physianAggregaton.Performance_rate, 100, 2)
Run Code Online (Sandbox Code Playgroud)
和
physianAggregaton.Performance_rate.ToString(100, 2)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2950 次 |
| 最近记录: |