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

AutoInclude causes ExecuteUpdate/Delete to have useless JOIN #29992

Open
roji opened this issue Jan 5, 2023 · 5 comments
Open

AutoInclude causes ExecuteUpdate/Delete to have useless JOIN #29992

roji opened this issue Jan 5, 2023 · 5 comments
Assignees
Labels
area-bulkcud punted-for-8.0 Originally planned for the EF Core 8.0 (EF8) release, but moved out due to resource constraints. type-bug
Milestone

Comments

@roji
Copy link
Member

roji commented Jan 5, 2023

With 8.0.0-alpha.1.23055.2, having an AutoInclude navigation on the target entity type causes ExecuteDelete to fail. Note that ExecuteUpdate does work correctly, and having an owned entity type also works (see related #28727):

await using var ctx = new BlogContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();

ctx.Blogs.ExecuteUpdate(s => s.SetProperty(b => b.Name, "Foo"));

public class BlogContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlServer(@"Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false")
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>(
            x => x.Navigation(b => b.Details).AutoInclude());
    }
}

public class Blog
{
    public int Id { get; set; }
    public string? Name { get; set; }

    public BlogDetails Details { get; set; }
}

public class BlogDetails
{
    public int Id { get; set; }
    public int Foo { get; set; }
}
@roji
Copy link
Member Author

roji commented Jan 5, 2023

Note that ExecuteUpdate works, but the SQL contains the useless JOIN from the AutoInclude:

UPDATE [b]
SET [b].[Name] = N'foo'
FROM [Blogs] AS [b]
INNER JOIN [BlogDetails] AS [b0] ON [b].[DetailsId] = [b0].[Id]

This also doesn't happen with an owned entity, so I suspect the same origin.

@roji roji changed the title AutoInclude causes ExecuteDelete to fail AutoInclude causes ExecuteDelete to fail and ExecuteUpdate to have useless JOIN Jan 5, 2023
@ajcvickers ajcvickers added this to the 8.0.0 milestone Jan 12, 2023
@amyboose

This comment was marked as off-topic.

@roji

This comment was marked as off-topic.

@roji roji changed the title AutoInclude causes ExecuteDelete to fail and ExecuteUpdate to have useless JOIN AutoInclude causes ExecuteUpdate to have useless JOIN Mar 24, 2023
@roji
Copy link
Member Author

roji commented Mar 24, 2023

Note the interesting difference between owned and auto-include in the pre-translation query trees (/cc @maumar):

Owned:

[Microsoft.EntityFrameworkCore.Query.EntityQueryRootExpression]
    .Select(b => [Microsoft.EntityFrameworkCore.Query.IncludeExpression])
    .ExecuteUpdate(s => s.SetProperty(b => b.Name, "Foo"))

Auto-include:

[Microsoft.EntityFrameworkCore.Query.EntityQueryRootExpression]
    .Join([Microsoft.EntityFrameworkCore.Query.EntityQueryRootExpression], b => Property(b, "DetailsId"), b0 => Property(b0, "Id"), (o, i) => new TransparentIdentifier`2(Outer = o, Inner = i))
    .Select(b => [Microsoft.EntityFrameworkCore.Query.IncludeExpression])
    .ExecuteUpdate(s => s.SetProperty(b => b.Name, "Foo"))

@roji
Copy link
Member Author

roji commented Mar 24, 2023

Note: split the Delete issue out to #30572

@roji roji changed the title AutoInclude causes ExecuteUpdate to have useless JOIN AutoInclude causes ExecuteUpdate/Delete to have useless JOIN Mar 24, 2023
@ajcvickers ajcvickers added the punted-for-8.0 Originally planned for the EF Core 8.0 (EF8) release, but moved out due to resource constraints. label Jun 28, 2023
@ajcvickers ajcvickers modified the milestones: 8.0.0, Backlog Jun 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-bulkcud punted-for-8.0 Originally planned for the EF Core 8.0 (EF8) release, but moved out due to resource constraints. type-bug
Projects
None yet
Development

No branches or pull requests

3 participants