Automatically include all relationships when using Include on a navigation declared on the base type
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- PR merge metrics
- PR metrics pending
Description
Hello,
I have one more bug with my favorite model :)
short description:
Following code should change column names for tables of Dividend and Coupon entity, but it doesn't.
Instead, it changes the column name of parent InvestmentYield entity.
```cs
modelBuilder.Entity().UseTptMappingStrategy();
modelBuilder.Entity().Property(d => d.InvestmentId)
.HasColumnName("stock_id");
modelBuilder.Entity().Property(c => c.InvestmentId)
.HasColumnName("bond_id");
```
Code for full repro
```cs
using Microsoft.EntityFrameworkCore;
public class Bond : Investment
{
public virtual ICollection Coupons { get; set; }
}
public class Coupon : InvestmentYield
{
}
public class Dividend : InvestmentYield
{
}
public abstract class Investment
{
public int Id { get; set; }
}
public abstract class InvestmentYield
{
public int Id { get; set; }
public Investment Investment { get; set; }
public int InvestmentId { get; set; }
}
public class Stock : Investment
{
public virtual ICollection Dividends { get; set; }
}
public class SomeDbContext : DbContext
{
public DbSet Investments { get; set; }
public DbSet Stocks { get; set; }
public DbSet Bonds { get; set; }
public DbSet Yields { get; set; }
public DbSet Dividends { get; set; }
public DbSet Coupons { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlite(@"Data Source=database.dat")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity().UseTptMappingStrategy();
modelBuilder.Entity().Property(d => d.InvestmentId)
.HasColumnName("stock_id");
modelBuilder.Entity().Property(c => c.InvestmentId)
.HasColumnName("bond_id");
}
}
public class Program
{
public static async Task Main()
{
using (var context = new SomeDbContext())
{
Console.WriteLine("Building model...");
var model = context.Model;
var yields = context.Yields.Include(x => x.Investment)
.ToList();
Console.WriteLine("Model built.");
}
}
}
```
This creates following table structure
```
Yields
---
Id
**bond_id**
Dividends
---
Id
StockId
Coupons
---
Id
BondId
Investments
---
Id
Stocks
---
Id
Bonds
---
Id
```
Problem is the column `bond_id` in Yields table. The idea was to change name of columns in Coupons/Dividends tables (see mapping), but that didn't happen and instead of that, it renamed column in Yields table. I guess it used `bond_id` since it was the last statement.
### Include provider and version information
EF Core version: 7.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer (same happens also for Microsoft.EntityFrameworkCore.Sqlite, maybe it is provider independent issue)
Target framework: .NET 7
Operating system: Windows 10
IDE: Visual Studio 2022 17.4
Contributor guide
Assessment
This issue has not been assessed yet.