标签: dbfunctions

如何在EF 6 Code First中将SQL Server JSON_VALUE函数用于经典.NET

如何在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中实现呢?

c# sql-server json entity-framework dbfunctions

7
推荐指数
1
解决办法
1970
查看次数

将 EF Core 约定和 DBFunction 混合用于 JObject 属性

我有一个具有 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)

mapping entity-framework .net-core dbfunctions

6
推荐指数
1
解决办法
3324
查看次数

LinqPad 5:名称“ DbFunctions”在当前上下文中不存在

我试图在LinqPad(C#语句)中使用以下内容:

DbFunctions.TruncateTime(mvOutDt).ToString()
Run Code Online (Sandbox Code Playgroud)

似乎无法识别DbFunction。我添加了System.Data.Entity参考。我什至尝试使用来完全限定它System.Data.Entity.DbFunctions,但没有。

任何帮助表示赞赏。

c# linqpad dbfunctions

3
推荐指数
1
解决办法
2271
查看次数

DbFunctions DiffDays给出错误的答案

我想从数据库中获取列表,那里是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

请帮助我找出代码中出了什么问题?
谢谢

c# linq datetime dbfunctions

3
推荐指数
1
解决办法
1855
查看次数

使用 EF core 2.1 调用 DbFunction

我尝试调用存储在数据库中的标量函数。这是我的代码:

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,我的标量函数从未被调用。为什么我错过了? …

c# dbfunctions ef-core-2.1

3
推荐指数
1
解决办法
3489
查看次数