如何在EF 6 Code First中将SQL Server JSON_VALUE函数用于经典.NET?我发现我可以像这样在EF Core中做到这一点:
public static class JsonExtensions
{
public static string JsonValue(string column, [NotParameterized] string path)
{
throw new NotSupportedException();
}
}
// In OnModelCreating
modelBuilder.HasDbFunction(typeof(JsonExtensions).GetMethod(nameof(JsonExtensions.JsonValue)))
.HasName("JSON_VALUE")
.HasSchema("");
// And then the usage
var result = db.Blogs.Select(t => JsonExtensions.JsonValue(t.Log, "$.JsonPropertyName")).ToArray();
Run Code Online (Sandbox Code Playgroud)
但是,如何在经典.NET(以我的情况为4.6.1)中的EF 6中实现呢?
我有一个具有 JObject 类型属性的实体,并且我需要能够针对这些属性使用 DbFunctions。
当我执行时,项目抛出一个异常,指出 DbFunction 不允许使用 JObject 类型的参数。
该实体就像...
public class OrchestrationRun
{
public long Id { get; set; }
public JObject MetaData { get; set; }
public JObject SystemMetaData { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
DbContext看起来像......
public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions options)
: base(options)
{
}
public virtual DbSet<OrchestrationRun> OrchestrationRun { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new OrchestrationRunConfiguration());
// DbFunction mapping for JSON_VALUE
modelBuilder.HasDbFunction( typeof(MyDbContext).GetMethod(nameof(JsonValue)))
.HasName("JSON_VALUE")
.HasSchema("");
}
// …Run Code Online (Sandbox Code Playgroud) 我试图在LinqPad(C#语句)中使用以下内容:
DbFunctions.TruncateTime(mvOutDt).ToString()
Run Code Online (Sandbox Code Playgroud)
似乎无法识别DbFunction。我添加了System.Data.Entity参考。我什至尝试使用来完全限定它System.Data.Entity.DbFunctions,但没有。
任何帮助表示赞赏。
我想从数据库中获取列表,那里是MyDate今天或在哪里。
我写了下面的代码。
_Log("Now: " + DateTime.Now.ToString());
var v = db_TS.TS_Test.Where(x => DbFunctions.DiffDays(x.MyDate,DateTime.Now) < 2);
foreach (var item in v.ToList())
{
_Log("MyDate: " + item.MyDate.ToString());
}
Run Code Online (Sandbox Code Playgroud)
记录以下内容:
现在:2016/11/08 10:50:00
MyDate:2017/09/27 09:35:00
请帮助我找出代码中出了什么问题?
谢谢
我尝试调用存储在数据库中的标量函数。这是我的代码:
public class PronosticDbContext : DbContext
{
public PronosticDbContext(DbContextOptions<PronosticDbContext> options) : base(options)
{
}
[DbFunction(FunctionName = "GetPlayerPointsForDay", Schema = "dbo")]
public static int GetPlayerPointsForDay(int dayId, int userId) => throw new NotSupportedException();
}
Run Code Online (Sandbox Code Playgroud)
电话:
private IQueryable<PlayerRankingData> GetPlayerRanking(int? dayId)
{
return Context.Days.Where(x => x.Id == dayId.Value).Select(x => new PlayerRankingData
{
// Some properties
Users = x.Season.Users.Select(us => new PlayerRankingData.User
{
// Other properties
Last = PronosticDbContext.GetPlayerPointsForDay(x.Id, us.User.Id),
// Others again
}).OrderByDescending(u => u.Points).ThenByDescending(u => u.Last).ThenBy(u => u.Login)
});
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么,但我总是去NotSupportedException,我的标量函数从未被调用。为什么我错过了? …
dbfunctions ×5
c# ×4
.net-core ×1
datetime ×1
ef-core-2.1 ×1
json ×1
linq ×1
linqpad ×1
mapping ×1
sql-server ×1