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

How to specify an optional SplitToTable configuration #29652

Closed
gabrielmatrella opened this issue Nov 22, 2022 · 4 comments
Closed

How to specify an optional SplitToTable configuration #29652

gabrielmatrella opened this issue Nov 22, 2022 · 4 comments

Comments

@gabrielmatrella
Copy link

I need to configure fields in the splitted table as optional, to ef core prepare queries with left join instead inner, there is some way to achieve this now?

public void Configure(EntityTypeBuilder<Produto> entity)
    {
        entity.ToTable("produto");

        entity.SplitToTable("produto_fiscal", tableBuilder =>
        {
            tableBuilder.Property(p => p.DescricaoEstoque);
        });
      ...

The resulting query:

 SELECT p.produtoid
      FROM produto AS p
      INNER JOIN produto_fiscal AS p0 ON p.produtoid = p0.produtoid

EF Core version: 7.0.0
Database provider: (e.g. Npgsql)

@ajcvickers
Copy link
Contributor

This issue is lacking enough information for us to be able to fully understand what is happening. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

@gabrielmatrella
Copy link
Author

There it is:

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace EfCoreSplitToTableOptionalIssue;

public static class Config
{
    public static string ConnectionString = @"Server=localhost;Database=SplitToTableSample;TrustServerCertificate=True;Integrated Security=True";
}

public class Produto
{
    public int ProdutoId { get; set; }
    public string Descricao { get; set; } = "";
    public string? DescricaoEstoque { get; set; }
}

public class SomeDbContext : DbContext
{
    public DbSet<Produto> Produtos { get; set; }
    
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlServer(Config.ConnectionString)
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Produto>(entity =>
        {
            entity.ToTable("produto")
                .SplitToTable("produto_fiscal", builder =>
                {
                    builder.Property(p => p.DescricaoEstoque);
                });
            
            entity.HasKey(x => x.ProdutoId);
            
            entity.Property(x => x.ProdutoId)
                .ValueGeneratedOnAdd();
        });
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        using var dbContext = new SomeDbContext();
        dbContext.Database.EnsureDeleted();
        dbContext.Database.EnsureCreated();
        
        dbContext.Produtos.Add(new Produto()
        {
            Descricao = "My Product"
        });

        dbContext.SaveChanges();
        
        dbContext.Produtos.ToList();
    }
}

Resulting query:

SELECT [p].[ProdutoId], [p].[Descricao], [p0].[DescricaoEstoque]
FROM [produto] AS [p]
INNER JOIN [produto_fiscal] AS [p0] ON [p].[ProdutoId] = [p0].[ProdutoId]

It works well in an environment where all records are created with the existence of the splittedtable at the beginning, but when you do mutation to your database adding some splittedtable after you already have records, its not possible to get results, unless executing some kind of script to create rows for every record in the new table during migration process.

Is there any configuration for the split table, where I can inform that it may not have records? (Left join)

@ajcvickers
Copy link
Contributor

@gabrielmatrella We will discuss.

@AndriySvyryd
Copy link
Member

Duplicate of #27974

@AndriySvyryd AndriySvyryd marked this as a duplicate of #27974 Nov 28, 2022
@AndriySvyryd AndriySvyryd closed this as not planned Won't fix, can't repro, duplicate, stale Nov 28, 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

3 participants