EntityFrameworkCore MySql包含未转换为In

时间:2017-08-09 07:11:21

标签: mysql entity-framework .net-core entity-framework-core

我正在开发一个使用Pomelo.EntityFrameworkCore.MySql(1.1.2)进行数据库访问的项目。

在我的查询中,我使用Contains()来检查特定列的值与可能值的列表。但是当我尝试执行下面显示的查询时,库无法将其转换为WHERE ... IN子句。这将显示在日志中,如图所示。

The LINQ expression '{__lookup_0 => Contains((?[x.B].CId?))}' could not be translated and will be evaluated locally.
The LINQ expression 'Contains((?[x.B].CId?))' could not be translated and will be evaluated locally.

这是这个特定图书馆的问题还是我错过了什么?

我添加了代码以便复制问题。

public class A
{
    public string Id { get; set; }

    [ForeignKey("B")]
    public string BId { get; set; }

    public virtual B B { get; set; }
}

public class B
{
    public string Id { get; set; }
    public string CId { get; set; }
}

public class MyContext : DbContext
{
    public DbSet<A> As { get; set; }

    public DbSet<B> Bs { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var loggerFactory = new LoggerFactory();
        loggerFactory.AddProvider(new EFLogProvider());

        optionsBuilder.UseMySql(@"Server=localhost;database=ef;uid=root;pwd=password;");
        optionsBuilder.UseLoggerFactory(loggerFactory);
    }
}

public class EFLogger : ILogger
{
    public IDisposable BeginScope<TState>(TState state)
    {
        return null;
    }

    public bool IsEnabled(LogLevel logLevel)
    {
        return true;
    }

    public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception,
        Func<TState, Exception, string> formatter)
    {
        Console.WriteLine(formatter(state, exception));
    }
}

public class EFLogProvider : ILoggerProvider
{
    public ILogger CreateLogger(string categoryName)
    {
        return new EFLogger();
    }

    public void Dispose()
    {

    }
}

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

            var a = new A { B = new B { CId = "test" } };

            context.Add(a);

            context.SaveChanges();

            var lookup = new[] {"test", "test2"};

            var res = context.As.Where(x => lookup.Contains(x.B.CId));

            Console.WriteLine(JsonConvert.SerializeObject(res));

        }

        Console.Read();
    }
}

提前感谢您的帮助!

此致

修改

正如评论中所建议的那样,修复了EF Core 2.0.0版本

0 个答案:

没有答案