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

FromSqlRaw with Owned Types generates complex and inefficient sql #23169

Closed
darkflame0 opened this issue Nov 2, 2020 · 1 comment
Closed

Comments

@darkflame0
Copy link

Steps to reproduce

    public class Entity
    {
        public int Id { get; set; }
        public string Prop { get; set; }
        public InnerEntity Inner { get; set; }
    }

    [Owned]
    public class InnerEntity
    {
        [Column(name:nameof(InnerProp))]
        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);
        }
    }

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

SQL

--  db.Entities.FromSqlRaw("select * from Entities").Select(a => a.Inner.InnerProp).ToList();
      SELECT [t].[InnerProp]
      FROM (
          select * from Entities
      ) AS [e]
      LEFT JOIN (
          SELECT [e0].[Id], [e0].[InnerProp]
          FROM [Entities] AS [e0]
          INNER JOIN [Entities] AS [e1] ON [e0].[Id] = [e1].[Id]
          WHERE [e0].[InnerProp] IS NOT NULL
      ) AS [t] ON [e].[Id] = [t].[Id]



--  db.Entities.Select(a => a.Inner.InnerProp).ToList();
      SELECT [e].[InnerProp]
      FROM [Entities] AS [e]

Development Environment

EF Core version: 5.0-rc2
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET CORE 3.1
Operating system: Windows 10
IDE: Visual Studio 2019 16.7.7

@ajcvickers
Copy link
Member

@darkflame0 Answered here: #18299 (comment)

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants