-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
TPT: Base type key field name is taken from derived type #23438
Comments
Here is a small project that reproduces the issue. using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace EFCoreBug
{
class Program
{
class TestDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;initial catalog=EFC5_TPT;Integrated Security=true");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Identity>().Property(u => u.Id).HasColumnName("IdentityId");
modelBuilder.Entity<User>().Property(u => u.Id).HasColumnName("UserId");
}
public DbSet<Identity> Identities { get; set; }
public DbSet<User> Users { get; set; }
}
[Table("Identity")]
class Identity
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
[Table("User")]
class User : Identity
{
public string Email { get; set; }
}
static void Main(string[] args)
{
}
}
} Migration: protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Identity",
columns: table => new
{
UserId = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Name = table.Column<string>(type: "nvarchar(max)", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Identity", x => x.UserId);
});
migrationBuilder.CreateTable(
name: "User",
columns: table => new
{
UserId = table.Column<int>(type: "int", nullable: false),
Email = table.Column<string>(type: "nvarchar(max)", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_User", x => x.UserId);
table.ForeignKey(
name: "FK_User_Identity_UserId",
column: x => x.UserId,
principalTable: "Identity",
principalColumn: "UserId",
onDelete: ReferentialAction.Restrict);
});
} |
Duplicate of #19811 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Context
I have to map an existing database to EFC5, and I want to take advantage of the Table-Per-Type feature.
Consider the following two tables, paying attention to the names of the PK fields:
They are mapped to these entities:
The column names are declared here:
Issue
EFC5 considers that the actual PK field name for the table 'Identity' is 'UserId' instead of 'IdentityId' as declared.
Any query will fail, and that behavior can be exposed when generating a migration:
This database was actually previously mapped to an EF6 model, using the TPT feature for these tables.
Provider and version information
EF Core version: 5.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 5.0
Operating system: Windows 10.0.19041.572
IDE: Visual Studio 2019 16.8.2
The text was updated successfully, but these errors were encountered: