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

Support rewriting primary key name with TPT #57

Open
roji opened this issue Jan 9, 2021 · 11 comments
Open

Support rewriting primary key name with TPT #57

roji opened this issue Jan 9, 2021 · 11 comments
Labels
blocked bug Something isn't working
Milestone

Comments

@roji
Copy link
Member

roji commented Jan 9, 2021

Rewriting is currently disabled for primary key names with TPT, because of dotnet/efcore#23444 (dotnet/efcore#19811).

@roji roji added the bug Something isn't working label Jan 9, 2021
@ghost
Copy link

ghost commented Jan 30, 2021

Is this being looked into? It is a fairly critical feature for us, we have a whole existing database model we are trying to connect to using ef core and this is a blocking issue. Can be easily reproduced with the following SQL model derived from the documentation itself:

CREATE TABLE [Blog] (
  [PkBlogId] int NOT NULL IDENTITY,
  [Url] nvarchar(max) NULL,
  CONSTRAINT [PK_Blogs] PRIMARY KEY ([PkBlogId])
);

CREATE TABLE [RssBlog] (
  [FkBlogId] int NOT NULL,
  [RssUrl] nvarchar(max) NULL,
  CONSTRAINT [PK_RssBlogs] PRIMARY KEY ([FkBlogId]),
  CONSTRAINT [FK_RssBlogs_Blogs_BlogId] FOREIGN KEY ([FkBlogId]) REFERENCES [Blogs] ([PkBlogId]) ON DELETE NO ACTION
);

@roji
Copy link
Member Author

roji commented Jan 31, 2021

@DMX-David-Cardinal as written above, the root issue is missing support in EF Core itself - tracked by dotnet/efcore#19811. In the meantime, you can explicitly name your PKs using the Fluent API - this should unblock you.

@ghost
Copy link

ghost commented Feb 1, 2021

When you say "the issue is missing support", I understand that no one is working on this at the moment and there is no estimate about when this will be ready?

Also, can you precise what you mean by explicitly naming the PKs using the fluent API? Because from what I tried it seemed like an impass though I might be missing something.

When you say explicitly naming the Pk, this is what I understand:

modelBuilder.Entity<RssBlog>(entity =>
{
  entity.Property(e => e.PkBlogId).HasColumnName("FkBlogId");
}

Yet this gives the following error: "Invalid column name 'FkBlogId'" (It isn't a typo; if I change the F for a P in both the database and the model and remove that line in the modelBuild, everything seems to run fine and as intended)

This would be the class definitions:

public partial class Blog
{
  public int PkBlogId { get; set; }
  public string Url { get; set; }
}

public partial class RssBlog : Blog
{
  public string RssUrl { get; set; }
}

@roji
Copy link
Member Author

roji commented Feb 1, 2021

When you say "the issue is missing support", I understand that no one is working on this at the moment and there is no estimate about when this will be ready?

dotnet/efcore#19811 is in the 6.0.0 milestone, meaning that we plan to do it for the next release of EF Core, planned for November.

When you say explicitly naming the Pk, this is what I understand:

This isn't about naming the column that's part of the PK, it's about the name of the PK itself, as in the docs. The issue is about having EFCore.NamingConventions rewrite that name, and that's blocked by dotnet/efcore#19811.

For your issue with changing HasColumnName, can you please open a new issue with a full code sample? Also, please test whether your issue is actually related to EFCore.NamingConventions; if not, please open your issue in https://github.com/dotnet/efcore/issues.

@ghost
Copy link

ghost commented Feb 1, 2021

dotnet/efcore#19811 is in the 6.0.0 milestone, meaning that we plan to do it for the next release of EF Core, planned for November.

My apologies, I wasn't familiar with the process.

This isn't about naming the column that's part of the PK, it's about the name of the PK itself, as in the docs. The issue is about having EFCore.NamingConventions rewrite that name, and that's blocked by dotnet/efcore#19811.

I don't see how to get around that since there is a clash between both PKs. Attempting to name the key on RssBlog gives the error A key cannot be configured on 'RssBlog' because it is a derived type. The key must be configured on the root type 'Blog'. From my understanding, this is blocking because the inherited column PkBlogId is a primary key on Blog, while the column FkBlogId is the primary key on RssBlog and both of them exist in the RssBlog class. I guess we will have to wait until november and hack our way around it in the meantime.

For your issue with changing HasColumnName, can you please open a new issue with a full code sample?

I don't think that's necessary.. It really seems like a byproduct of the column/key issues where either two properties are trying to map to the same column or the mapper tries to find PkBlogId in RssBlog which doesn't exist or whatnot.

@roji
Copy link
Member Author

roji commented Feb 1, 2021

@DMX-David-Cardinal just to make sure, it's perfectly OK to set the key name in a TPT hierarchy - but you have to do it at the root. Being able to set it on derived types would be part of dotnet/efcore#19811.

@ghost
Copy link

ghost commented Feb 3, 2021

Oh! Misunderstood the difference between the two issues. Apologies once again

@gojanpaolo

This comment has been minimized.

@roji

This comment has been minimized.

@gojanpaolo

This comment has been minimized.

@roji

This comment was marked as resolved.

@roji roji added the blocked label Jan 3, 2024
@roji roji added this to the Backlog milestone Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants