带有动态字符串“ Where”查询的EF Core产生InvalidOperationException

时间:2020-10-20 17:02:22

标签: c# ef-core-3.1

我们正在使用EF Core 3.1,并尝试从字符串创建动态的“ where”查询。 我们使用了一个简单的测试应用,可以根据以下directions进行生成。

对于动态查询,请转到following,获取文件Dynamic.cs: 并搜索Dynamic.cs,或转到此link

对于.net核心,请使用nuget添加以下程序包System.Reflection.Emit。 在原始文章中,我们将实体替换为Model.cs:

public class MyContext : DbContext
{
    public DbSet<Student> Students { get; set; }
    public DbSet<Status> Statuses { get; set; }

    protected override void OnConfiguring ( DbContextOptionsBuilder options )
        => options.UseSqlite ( "Data Source=students.db" );
}

public class Status
{
    public int StatusId
    { get; set; }

    public string StatusName
    { get; set; }
}

public class Student
{
    public int StudentId
    { get; set; }

    public string FirstName
    { get; set; }

    public string LastName
    { get; set; }

    public Status Status
    { get; set; }
    
}

然后我们用此代码修改了Program.cs。注释掉的代码将生成数据库。

class Program
{
    static void Main ( string [] args )
    {
        /*
        using ( var context = new MyContext () )
        {
            context.Database.EnsureCreated ();

            var goodStatus = new Status () { StatusId = 1, StatusName = "Good" };
            var badStatus = new Status () { StatusId = 2, StatusName = "Bad" };

            
            context.Statuses.Add ( goodStatus );
            context.Statuses.Add ( badStatus );
            context.SaveChanges ();

            

            context.Students.Add (
                new Student () { StudentId = 1, FirstName="Joe", LastName="Smith", Status = goodStatus } );

            context.Students.Add (
                new Student () { StudentId = 2, FirstName="John", LastName="Doe", Status = badStatus } );

            context.SaveChanges ();


            var students = context.Students.ToList ();
        }
        */

        try
        {
            var filterExpression = "Status.StatusName!=null && Status.StatusName==\"In-Active\"";

            using ( var context = new MyContext () )
            {

                var res =
                    ( from c in
                        context.Students.
                            Include ( g => g.Status ).
                            AsNoTracking ()
                      select c ).Where ( filterExpression ).ToList ();

            }
        }
        catch ( Exception ex )
        {
            var msg = ex.Message;
        }

    }
}

下面的数据库创建代码是我们要运行的查询。

        var filterExpression = "Status.StatusName!=null && Status.StatusName==\"Good\"";

        using ( var context = new MyContext () )
        {

            var res =
                ( from c in
                    context.Students.
                        Include ( g => g.Status ).
                        AsNoTracking ()
                  select c ).Where ( filterExpression ).ToList ();

        }

在Program.cs和Model.cs中,我们都使用以下命名空间:

using System;
using System.Collections.Generic;
using System.Linq;

using Microsoft.EntityFrameworkCore;

使用诸如“ Status.StatusName!= null && Status.StatusName ==“ Good””之类的动态查询,我们执行查询:这将产生InvalidOperationException“ Sql Tree中的Null Type mapping”。 我们需要做些什么来纠正这个问题?

更新

我发现下载了nuget软件包Microsoft.EntityFrameworkCore.DynamicLinq并注释掉了Dynamic.cs代码,并使用一条语句添加了它:

using System.Linq.Dynamic.Core;

并且能够运行并获得结果。

https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.DynamicLinq

0 个答案:

没有答案