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

Alternate key in TPT Table is dropped in the same migration it was created. #22942

Closed
RonOsmo opened this issue Oct 10, 2020 · 10 comments
Closed

Comments

@RonOsmo
Copy link

RonOsmo commented Oct 10, 2020

File a bug

I am trying to get EFCore 5 RC to populate a portion of our database, the application is a legacy monolithic web app.
The trouble is I am trying to add base classes with TPT. Of course I have used the ToTable() function to separate the tables.
For example there are several hierarchies:

class Employee : User
{
        public int EmployeeID { get; set; }
        ...
}
c;lass User : Contact
{
         public int UserID { get; set; }
         ...
}
class Contact 
{
        public int ContactID { get; set; }
        ...
}

EFCore does not honor these primary keys on the Employee or User classes but always seems to drop the original primary key and to add an alternate key named ContactID. I have tried hard to find a way around the problem but to no avail !

The latest migration looks wrong to me.

Include your code

Phase1.zip

Include provider and version information

EF Core version: 5.0.0-rc.1.20451.13
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 3.1.0
Operating system: Windows [Version 10.0.19041.508]
IDE: Visual Studio 2019 16.7.5

@RonOsmo RonOsmo changed the title TPT Tables do not honor the PrimaryKey column on the Inheriting table, instead creating an alternate key whose Column name is different from the original. TPT Tables do not honor the PrimaryKey column on the Inheriting table, instead creating an alternate key. Oct 10, 2020
@AndriySvyryd
Copy link
Member

If I understand correctly you want to have different names for the primary key columns in different tables mapped to the hierarchy. #19811 will enable this.

@RonOsmo
Copy link
Author

RonOsmo commented Oct 11, 2020

@AndriySvyryd

Thank you.

That kind of works - the important thing is that we are using the same named Column name as we had, but the column in the inheriting table does not update the property with the value, can we get it to do that as well? For instance I have a User relation which i want to inherit Contact. But I want a named property of UserId to be used instead of ContactId (yes I can specify the "UserId" as the column name for the shadow property called "ContactId"), but can I get the TPT feature to simply use the property 'UserId' instead of the shadow property ? This is the normal way we do TPT in our monolith and there are many of these examples. Some do use the convention of using the base table's primary key name on both sides of the relation, but many do not.

And I wish that the TPT feature did not delete the inheriting table's PrimaryKey and replace it with an AlternateKey why? It also replaces an AlternateKey on the base table. The following code gets ignored/deleted in the migration.

           modelBuilder.Entity<Contact>(entity =>
            {
                entity.ToTable("Contact");
                entity.HasAlternateKey(e => new { e.ContactName, e.ContactId })
                    .HasName("iContact-ContactName");
            }

@RonOsmo
Copy link
Author

RonOsmo commented Oct 11, 2020

@AndriySvyryd

Is this code not in included in EF Core version: 5.0.0-rc.1.20451.13 ?

@AndriySvyryd
Copy link
Member

But I want a named property of UserId to be used instead of ContactId (yes I can specify the "UserId" as the column name for the shadow property called "ContactId"), but can I get the TPT feature to simply use the property 'UserId' instead of the shadow property ?

#19811 should enable this too.

And I wish that the TPT feature did not delete the inheriting table's PrimaryKey and replace it with an AlternateKey why? It also replaces an AlternateKey on the base table.

Alternate keys aren't supported on derived types yet, see #2611. There might be a bug as EF should throw an exception for this instead for creating a different key, I'll investigate.

Is this code not in included in EF Core version: 5.0.0-rc.1.20451.13 ?

No, #19811 is not implemented yet.

@AndriySvyryd
Copy link
Member

AndriySvyryd commented Oct 12, 2020

The following code gets ignored/deleted in the migration.

In the "20201010045907_2 handle inheritence'" migration I can see

            migrationBuilder.AddUniqueConstraint(
                name: "iContact-ContactName",
                table: "Contact",
                columns: new[] { "ContactName", "ContactID" });

Why do you say that it's ignored?

@RonOsmo
Copy link
Author

RonOsmo commented Oct 12, 2020

@AndriySvyryd
Because later on its dropped !!! In the same migration script.

            migrationBuilder.DropPrimaryKey(
                name: "PK_Country",
                table: "Country");

           migrationBuilder.DropUniqueConstraint(
                name: "iContact-ContactName",
                table: "Contact");

            migrationBuilder.DropPrimaryKey(
                name: "PK_CompanyAddress",
                table: "CompanyAddress");

            migrationBuilder.DropColumn(
                name: "UserID",
                table: "User");

Is there a way i can get a version of EF5 with the changes for #19811 ?

@AndriySvyryd
Copy link
Member

I see,

Is there a way i can get a version of EF5 with the changes for #19811 ?

No, that feature hasn't been implemented yet.

@AndriySvyryd AndriySvyryd reopened this Oct 12, 2020
@AndriySvyryd AndriySvyryd changed the title TPT Tables do not honor the PrimaryKey column on the Inheriting table, instead creating an alternate key. Alternate key in TPT Table is dropped in the same migration it was created. Oct 12, 2020
@RonOsmo
Copy link
Author

RonOsmo commented Oct 12, 2020

@AndriySvyryd
Thanks for looking at my concerns. Will we see the fixes in EF6 ?

@AndriySvyryd
Copy link
Member

@RonOsmo Yes, that's the current plan.

@AndriySvyryd
Copy link
Member

AndriySvyryd commented Oct 13, 2020

Because later on its dropped !!! In the same migration script.

It's dropped in the Down method. This is by design. If you still have issues with alternate keys on the base type please post a minimal repro.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 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