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 change the primary key column name for a derived type using TPT inheritance strategy #31189

Closed
petarpetrovt opened this issue Jul 5, 2023 · 2 comments

Comments

@petarpetrovt
Copy link

petarpetrovt commented Jul 5, 2023

By default the derived type expects a column for the primary key with the same name as the base type. Is it possible to change the column name only for the derived type table?

public class ObjectEntity
{
  public int Id { get; set; } 
  public DateTime DateCreated { get; set; }
}

public class MyObjectEntity : ObjectEntity
{
  public int ObjectId { get => Id; set => Id = value; } 
  public Guid? Guid { get; set; }
}

builder.Entity<ObjectEntity>(e =>
{
  e.ToTable("Objects", "dbo");
  e.Property(x => x.Id).HasColumnType("int").HasColumnName("Id").UseIdentityColumn();
  e.Property(x => x.DateCreated).HasColumnType("datetime").HasColumnName("DateCreated").IsRequired(true)
    .HasDefaultValueSql("GETDATE()").ValueGeneratedOnAdd();
  e.HasKey(x => x.Id);
});

builder.Entity<MyObjectEntity>(e =>
{
  e.ToTable("MyObjects", "dbo");

  // This changes it for both tables not only for `dbo.MyObjects`
  e.Property(x => x.Id).HasColumnName("ObjectId");

  // This expects both columns (Id, ObjectId)
  //e.Property(x => x.ObjectId).HasColumnType("int").HasColumnName("ObjectId");

  e.Property(x => x.Guid).HasColumnType("uniqueidentifier").HasColumnName("Guid").IsRequired(false);
  e.HasBaseType<ObjectEntity>();
});

Actual tables:

dbo.Objects -> (ObjectId, DateCreated)
dbo.MyObjects -> (ObjectId, Guid)

Expected tables:

dbo.Objects -> (Id, DateCreated)
dbo.MyObjects -> (ObjectId, Guid)

@ajcvickers
Copy link
Contributor

Duplicate of #19811

@ajcvickers ajcvickers marked this as a duplicate of #19811 Jul 5, 2023
@petarpetrovt
Copy link
Author

Thanks

// Before
e.ToTable("MyObjects", "dbo");
e.Property(x => x.Id).HasColumnName("ObjectId");
e.Property(x => x.Guid).HasColumnType("uniqueidentifier").HasColumnName("Guid").IsRequired(false);

// After
e.ToTable("MyObjects", "dbo", tb => tb.Property(x => x.Id).HasColumnName("ObjectId"));
e.Property(x => x.Guid).HasColumnType("uniqueidentifier").HasColumnName("Guid").IsRequired(false);

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Jul 5, 2023
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