Skip to content
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

FromSql over entity type when selecting data from owned reference navigations #21769

Closed
darkflame0 opened this issue Jul 24, 2020 · 3 comments · Fixed by #21778
Closed

FromSql over entity type when selecting data from owned reference navigations #21769

darkflame0 opened this issue Jul 24, 2020 · 3 comments · Fixed by #21778
Assignees
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@darkflame0
Copy link

darkflame0 commented Jul 24, 2020

System.InvalidCastException: Unable to cast object of type 'Microsoft.EntityFrameworkCore.Query.SqlExpressions.FromSqlExpression' to type 'Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression'.
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.WeakEntityExpandingExpressionVisitor.GetPropertyExpressionsFromSubquery(IEntityType entityType, ColumnExpression identifyingColumn, Boolean nullable)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.WeakEntityExpandingExpressionVisitor.TryExpand(Expression source, MemberIdentity member)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.WeakEntityExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at Microsoft.EntityFrameworkCore.Query.IncludeExpression.VisitChildren(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.VisitExtension(Expression node)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.WeakEntityExpandingExpressionVisitor.VisitExtension(Expression extensionExpression)
   at System.Linq.Expressions.Expression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.WeakEntityExpandingExpressionVisitor.Expand(SelectExpression selectExpression, Expression lambdaBody)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.ExpandWeakEntities(SelectExpression selectExpression, Expression lambdaBody)
   at Microsoft.EntityFrameworkCore.Query.Internal.RelationalProjectionBindingExpressionVisitor.Translate(SelectExpression selectExpression, Expression expression)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateSelect(ShapedQueryExpression source, LambdaExpression selector)
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at DbTest.Program.Main(String[] args) in D:\zhaoyl\source\repos\DbTest\DbTest\Program.cs:line 13

Steps to reproduce

    class Program
    {
        static void Main(string[] args)
        {
            using var db = new TestDbContext();
            db.Database.EnsureCreated();
            db.Entities.FromSqlRaw("select * from Entities").Where(a => a.Prop == "111").ToList();
            Console.ReadKey();
        }
    }

    public class Entity
    {
        public int Id { get; set; }
        public string Prop { get; set; }
        public Inner Inner { get; set; }
    }
    [Owned]
    public class Inner
    {
        public string InnerProp { get; set; }
    }
    public class TestDbContext : DbContext
    {
        public static readonly ILoggerFactory MyLoggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); });
        public DbSet<Entity> Entities { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=TestDb;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False")
            .UseLoggerFactory(MyLoggerFactory);
        }
    }

Further technical details

EF Core version: 5.0.0-preview.7.20365.15
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.1
Operating system: Windows 10
IDE: Visual Studio 2019 16.6.4

@smitpatel
Copy link
Member

The posted repro code works fine for me in 5.0.0-preview.7.20365.15

@smitpatel smitpatel changed the title FromSqlRaw Composing with Where clause doesn't work Disallow FromSql over entity type when selecting data from owned reference navigations Jul 24, 2020
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jul 24, 2020
smitpatel added a commit that referenced this issue Jul 24, 2020
…ame table

Resolves #21769

It would still work if owned navigation data is not accessed. Though keep in mind that selecting entity will bring owned data too due to auto include
smitpatel added a commit that referenced this issue Jul 24, 2020
…ame table

Resolves #21769

It would still work if owned navigation data is not accessed. Though keep in mind that selecting entity will bring owned data too due to auto include
smitpatel added a commit that referenced this issue Jul 25, 2020
…ame table

Resolves #21769

It would still work if owned navigation data is not accessed. Though keep in mind that selecting entity will bring owned data too due to auto include
@ghost ghost closed this as completed in #21778 Jul 25, 2020
ghost pushed a commit that referenced this issue Jul 25, 2020
…ame table (#21778)

Resolves #21769

It would still work if owned navigation data is not accessed. Though keep in mind that selecting entity will bring owned data too due to auto include
@darkflame0
Copy link
Author

darkflame0 commented Jul 26, 2020

The posted repro code works fine for me in 5.0.0-preview.7.20365.15

sorry,my bad,I've updated the code

@smitpatel smitpatel changed the title Disallow FromSql over entity type when selecting data from owned reference navigations FromSql over entity type when selecting data from owned reference navigations Aug 1, 2020
@smitpatel
Copy link
Member

smitpatel commented Aug 1, 2020

We are going to revert the change this bought and allow FromSql on owner same way it worked in 3.1 by introducing join with the table for owned entity data.

See also #21781 #21888

@smitpatel smitpatel modified the milestones: 5.0.0, 5.0.0-preview8, 5.0.0-rc1 Aug 1, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0-rc1, 5.0.0 Nov 7, 2020
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants