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

OwnedType does not work with type configured with .ToSqlQuery #34160

Closed
romfir opened this issue Jul 4, 2024 · 1 comment
Closed

OwnedType does not work with type configured with .ToSqlQuery #34160

romfir opened this issue Jul 4, 2024 · 1 comment

Comments

@romfir
Copy link

romfir commented Jul 4, 2024

When type configured using ToSqlQuery has an owned type, EF Core generates incorrect query

EF Core Config

async Task Main()
{
	var c = new Context();

	c.Centres.First(); //Invalid object name 'Centres'.
}

public class Context : DbContext
{
	public DbSet<Centre> Centres { get; set; }

	protected override void OnModelCreating(ModelBuilder modelBuilder)
	{
		base.OnModelCreating(modelBuilder);

		modelBuilder.Entity<Centre>(config =>
		{
			config.ToSqlQuery(@"SELECT c.Id, c.Name, cg.Guid FROM CENTRE c
			JOIN CentreGuid cg on cg.CentreId = c.Id
			");

			config.OwnsOne(c => c.Test, c =>
			{
				c.Property(c => c.Guid).HasColumnName("Guid");
			});
		});
	}

	protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
	{
		optionsBuilder
			.UseLazyLoadingProxies()
			.UseSqlServer(".")
			.EnableSensitiveDataLogging(true);
	}
}

public class Centre
{
	public int Id { get; set; }
	public string Name { get; set; }
	public GuidWrapper Test { get; set; }
}

public class GuidWrapper 
{
	public Guid Guid { get; set; }
}

Generated SQL

SELECT TOP(1) [u].[Id], [u].[Name], [c].[Id], [c].[Guid]
FROM (
    SELECT c.Id, c.Name, cg.Guid FROM CENTRE c
    			JOIN CentreGuid cg on cg.CentreId = c.Id
    			
) AS [u]
LEFT JOIN [Centres] AS [c] ON [u].[Id] = [c].[Id]

Here we can observe join to non existant table Centres. The join should eaither not be made and its data shoud be selected from the first part or it should be made to the same subquery as in the first part

Possibly releated issue: #29300

Include provider and version information

EF Core version:
Database provider: Microsoft.EntityFrameworkCore.SqlServer 8.0.4
Target framework: .NET 8.0

@ajcvickers
Copy link
Member

Duplicate of #14525

@ajcvickers ajcvickers marked this as a duplicate of #14525 Jul 7, 2024
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Jul 7, 2024
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