【发布时间】:2018-08-15 09:37:23
【问题描述】:
是否可以以在 Entity Framework 6 和 EF Core 中都适用的方式使用 .Include()?
我目前有可以访问IQueryable<> 但不知道其来源的命令处理程序,并且可以根据运行的上下文更改来源。例如,在运行实际应用程序时使用 Entity Framework 6,但使用 EF Core 进行测试(使用 SQLite In Memory 提供程序)
using System.Data.Entity;
class DemoCommandHandler
{
public void Handle(IQueryable<Blog> blogsQueryable, DemoCommand command)
{
//Need to get the blog and all the posts here
var blogs = blogsQueryable.Include(b => b.Posts).Single(b => b.Id = command.BlogId);
//Do stuff with blog and posts
}
}
以上在使用 Entity Framework 时工作正常,但在运行 EF Core 时.Include() 不起作用(注意using System.Data.Entity);
如果将 using 语句更改为 using Microsoft.EntityFrameworkCore,则它在运行 EF Core 而不是 Entity Framework 时有效。
有没有办法让框架不可知.Include()?或者至少同时支持 EF Core 和 Entity Framework?
【问题讨论】:
-
您的问题回答了我的问题 - “如果将 using 语句更改为使用 Microsoft.EntityFrameworkCore 则它在运行 EF Core 时有效” - 谢谢!
标签: c# entity-framework ef-core-2.0 ef-core-2.1