-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Passing in QueryModel from re-linq #4417
Comments
@biqas var serviceProvider = context.GetInfrastructure<IServiceProvider>();
var factory = serviceProvider.GetService<IEntityQueryModelVisitorFactory>(); |
Out of interest, what are you trying to do? |
@rowanmiller Basically in one of the steps I'm analyzing amd striping away some partial linq statements and adding service specific linq statements and it is much easier to do so in re-linq. Currently the process is: Hope this makes it some how understandable? :) |
We discussed this more today, it's probably simpler to get the |
Hi, that sounds good, I will try it out in the next days. |
Feel free to reopen if you hit issues |
Hi, using (var db = new BloggingContext())
{
var serviceProvider = ((IInfrastructure<IServiceProvider>)db).Instance;
var database = (IDatabase)serviceProvider.GetService(typeof(IDatabase));
var queryContextFactory = (IQueryContextFactory)serviceProvider.GetService(typeof(IQueryContextFactory));
var query = new EnumerableQuery<Blog>(new List<Blog>())
.Where(x => x.Url != "");
var queryModel = QueryParser.CreateDefault().GetParsedQuery(query.Expression);
var fromExpression = Expression.Constant(db.Blogs);
queryModel.MainFromClause.FromExpression = fromExpression;
var compileQueryFn = database.CompileQuery<Blog>(@queryModel);
var result = compileQueryFn(queryContextFactory.Create()).ToList();
} I'm getting the following error:
BloggingContext is taken from the EF documentation. Question is, is this the intended behavior if so, how can I get around it? |
It looks like the MainFromClause is a constant expression with a value of var fromExpression = Expression.Constant(db.Blogs); with: var fromExpression = Expression.Constant(new EntityQueryable<Blog>(db.GetService<IAsyncQueryProvider>())); The concurrency exception was basically a reentry test that was triggering because the query enumerated the inner query ( I don't know if it will work for your scenario, but you might try replacing the |
Hi, The problem wich is left over I think is not directly related to this issue, which is: public interface IBlog
{
int BlogId { get; set; }
string Url { get; set; }
}
public class Blog : IBlog
{
public int BlogId { get; set; }
public string Url { get; set; }
public List<Post> Posts { get; set; }
} Note the interface usage. using (var db = new BloggingContext())
{
var serviceProvider = ((IInfrastructure<IServiceProvider>)db).Instance;
var database = (IDatabase)serviceProvider.GetService(typeof(IDatabase));
var queryContextFactory = (IQueryContextFactory)serviceProvider.GetService(typeof(IQueryContextFactory));
var query = new EnumerableQuery<IBlog>(new List<IBlog>())
.Where(x => x.Url != "");
var queryModel = QueryParser.CreateDefault().GetParsedQuery(query.Expression);
var queryProvider = (IAsyncQueryProvider)serviceProvider.GetService(typeof(IAsyncQueryProvider));
var fromExpression = Expression.Constant(new EntityQueryable<IBlog>(queryProvider));
queryModel.MainFromClause.FromExpression = fromExpression;
var compileQueryFn = database.CompileQuery<Blog>(@queryModel);
var result = compileQueryFn(queryContextFactory.Create()).ToList();
} So far I know EF is currently not supporting interface definitions, I think I have to rewrite the places where interfaces are used within the QueryModel. Out of interest, why is EF not supporting or making use of structural matching for mappings and translating materialisation to types? |
I don't think I have the context to answer this question properly, clearing the milestone so this can go back to triage. |
Interfaces are not supported yet, we'll probably handle this as part of #240 where you can tell us how to create instances of a type for a given interface. |
Hi I faced one issue using (var db = new BloggingContext())
{
var serviceProvider = ((IInfrastructure<IServiceProvider>)db).Instance;
var database = (IDatabase)serviceProvider.GetService(typeof(IDatabase));
var queryContextFactory = (IQueryContextFactory)serviceProvider.GetService(typeof(IQueryContextFactory));
var query = new EnumerableQuery<IBlog>(new List<IBlog>())
.Where(x => x.Url != "")
.Select(x => x,Posts);
var queryModel = QueryParser.CreateDefault().GetParsedQuery(query.Expression);
var queryProvider = (IAsyncQueryProvider)serviceProvider.GetService(typeof(IAsyncQueryProvider));
var fromExpression = Expression.Constant(new EntityQueryable<IBlog>(queryProvider));
queryModel.MainFromClause.FromExpression = fromExpression;
var compileQueryFn = database.CompileQuery<Blog>(@queryModel);
var result = compileQueryFn(queryContextFactory.Create()).ToList();
}
if I change the projection to posts, then the call to
throws an error
Also tried to change the generic type parameter to:
Any recommendations? |
Not sure exactly what you are trying to query using (var db = new BloggingContext())
{
var serviceProvider = ((IInfrastructure<IServiceProvider>)db).Instance;
var database = (IDatabase)serviceProvider.GetService(typeof(IDatabase));
var queryContextFactory = (IQueryContextFactory)serviceProvider.GetService(typeof(IQueryContextFactory));
var query = new EnumerableQuery<Blog>(new List<Blog>())
.Where(x => x.Url != "")
.SelectMany(x => x.Posts);
var queryModel = QueryParser.CreateDefault().GetParsedQuery(query.Expression);
var queryProvider = (IAsyncQueryProvider)serviceProvider.GetService(typeof(IAsyncQueryProvider));
var fromExpression = Expression.Constant(new EntityQueryable<Blog>(queryProvider));
queryModel.MainFromClause.FromExpression = fromExpression;
var compileQueryFn = database.CompileQuery<Post>(@queryModel);
var result = compileQueryFn(queryContextFactory.Create()).ToList();
} Changes made:
This just throw compilation error on me. |
Feel free to re-open the issue if you encounter any further issues or have any questions. |
Hi,
is it possible to to have an public method which is accepting QueryModel as input parameter instead of Expression parameter?
The text was updated successfully, but these errors were encountered: