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

Weird behavior with generics #4470

Closed
joshmouch opened this issue Feb 2, 2016 · 3 comments
Closed

Weird behavior with generics #4470

joshmouch opened this issue Feb 2, 2016 · 3 comments

Comments

@joshmouch
Copy link

I'm not sure how I got into this state, but every time I do a dnx add migration, it is ignoring my actual changes, and instead, generating the following migration:

migrationBuilder.DropForeignKey(name: "FK_Guid>_Role_RoleId", table: "RoleClaims");
            migrationBuilder.DropForeignKey(name: "FK_Guid>_User_UserId", table: "UserClaims");
            migrationBuilder.DropForeignKey(name: "FK_Guid>_User_UserId", table: "UserLogins");
            migrationBuilder.DropForeignKey(name: "FK_Guid>_Role_RoleId", table: "UserRoles");
            migrationBuilder.DropForeignKey(name: "FK_Guid>_User_UserId", table: "UserRoles");
            migrationBuilder.DropPrimaryKey(name: "PK_Guid>", table: "UserRoles");
            migrationBuilder.DropPrimaryKey(name: "PK_Guid>", table: "UserLogins");
            migrationBuilder.DropPrimaryKey(name: "PK_Guid>", table: "UserClaims");
            migrationBuilder.DropPrimaryKey(name: "PK_Guid>", table: "RoleClaims");
            migrationBuilder.AddPrimaryKey(
                name: "PK_IdentityUserRole<Guid>",
                table: "UserRoles",
                columns: new[] { "UserId", "RoleId" });
            migrationBuilder.AddPrimaryKey(
                name: "PK_IdentityUserLogin<Guid>",
                table: "UserLogins",
                columns: new[] { "UserId", "LoginProvider", "ProviderKey" });
            migrationBuilder.AddPrimaryKey(
                name: "PK_IdentityUserClaim<Guid>",
                table: "UserClaims",
                column: "Id");
            migrationBuilder.AddPrimaryKey(
                name: "PK_IdentityRoleClaim<Guid>",
                table: "RoleClaims",
                column: "Id");
            migrationBuilder.AddForeignKey(
                name: "FK_IdentityRoleClaim<Guid>_Role_RoleId",
                table: "RoleClaims",
                column: "RoleId",
                principalTable: "Roles",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
            migrationBuilder.AddForeignKey(
                name: "FK_IdentityUserClaim<Guid>_User_UserId",
                table: "UserClaims",
                column: "UserId",
                principalTable: "Users",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
            migrationBuilder.AddForeignKey(
                name: "FK_IdentityUserLogin<Guid>_User_UserId",
                table: "UserLogins",
                column: "UserId",
                principalTable: "Users",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
            migrationBuilder.AddForeignKey(
                name: "FK_IdentityUserRole<Guid>_Role_RoleId",
                table: "UserRoles",
                column: "RoleId",
                principalTable: "Roles",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
            migrationBuilder.AddForeignKey(
                name: "FK_IdentityUserRole<Guid>_User_UserId",
                table: "UserRoles",
                column: "UserId",
                principalTable: "Users",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);

I can't actually commit that migration to the database, but if I comment it out and then do a "dnx ef database update" then the next time I do a "dnx ef migrations add", I'll get the exact same migration as above.

It looks like there's some problem with naming the FK/PK when it has a generic argument.

Here is the configuration for one of the above entities:

var entity = modelBuilder.Entity<IdentityUserRole<Guid>>();

// Primary Key
entity.HasKey(t => new { t.UserId, t.RoleId });

// Table & Column Mappings
entity.ToTable("UserRoles");
entity.Property(t => t.UserId).HasColumnName("UserId");
entity.Property(t => t.RoleId).HasColumnName("RoleId");
@divega
Copy link
Contributor

divega commented Feb 2, 2016

EF Core supports using closed generic types such as IdentityUserRole<Guid> directly as entities. When you do that, by default you will get names in the database that include the angle braces and generic arguments. We have found that at least in SQL Server those names work just fine but we need to quote them correctly. From that, and from reading the code in the migration you posted, I get that the two main problems with this migration could be that:

  1. Maybe we are not quoting all the names
  2. Some names seem to be mangled, e.g. FK_Guid>_Role_RoleId and all the other names in the drop methods.

Does that sound correct?

cc @bricelam

@joshmouch
Copy link
Author

I'm not sure about #1. It could be, but with the hacks I've made trying to get Identity working, I'm not sure.
#2 is correct, though.

FYI, I manually named the PK's and FK's as a workaround, and now migrations are working correctly.

@smitpatel
Copy link
Contributor

dupe of #3545 Already fixed and will be shipped in RC2

@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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants