Duplicated foreign keys after restoring missing IdentityUser and IdentityRole navigation properties
See original GitHub issuePreviously on 1.1.2 IdentityUser and IdentityRole provided the following navigation properties:
User
public virtual ICollection<UserRole> Roles { get; } = new List<UserRole>();
public virtual ICollection<UserClaim> Claims { get; } = new List<UserClaim>();
public virtual ICollection<UserLogin> Logins { get; } = new List<UserLogin>();
Role
public virtual ICollection<UserRole> Users { get; } = new List<UserRole>();
public virtual ICollection<RoleClaim> Claims { get; } = new List<RoleClaim>();
Now if I manually add them (like it’s written in 1.x to 2.0 migration docs) I get this when running migrations script:
CREATE TABLE [UserClaims] (
[Id] int NOT NULL IDENTITY,
[ClaimType] nvarchar(max) NULL,
[ClaimValue] nvarchar(max) NULL,
[UserId] int NOT NULL,
[UserId1] int NULL,
CONSTRAINT [PK_UserClaims] PRIMARY KEY ([Id]),
CONSTRAINT [FK_UserClaims_Users_UserId] FOREIGN KEY ([UserId]) REFERENCES [Users] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_UserClaims_Users_UserId1] FOREIGN KEY ([UserId1]) REFERENCES [Users] ([Id]) ON DELETE NO ACTION
);
CREATE TABLE [UserLogins] (
[LoginProvider] nvarchar(450) NOT NULL,
[ProviderKey] nvarchar(450) NOT NULL,
[ProviderDisplayName] nvarchar(max) NULL,
[UserId] int NOT NULL,
[UserId1] int NULL,
CONSTRAINT [PK_UserLogins] PRIMARY KEY ([LoginProvider], [ProviderKey]),
CONSTRAINT [FK_UserLogins_Users_UserId] FOREIGN KEY ([UserId]) REFERENCES [Users] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_UserLogins_Users_UserId1] FOREIGN KEY ([UserId1]) REFERENCES [Users] ([Id]) ON DELETE NO ACTION
);
CREATE TABLE [UserRoles] (
[UserId] int NOT NULL,
[RoleId] int NOT NULL,
[RoleId1] int NULL,
[UserId1] int NULL,
CONSTRAINT [PK_UserRoles] PRIMARY KEY ([UserId], [RoleId]),
CONSTRAINT [FK_UserRoles_Roles_RoleId] FOREIGN KEY ([RoleId]) REFERENCES [Roles] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_UserRoles_Roles_RoleId1] FOREIGN KEY ([RoleId1]) REFERENCES [Roles] ([Id]) ON DELETE NO ACTION,
CONSTRAINT [FK_UserRoles_Users_UserId] FOREIGN KEY ([UserId]) REFERENCES [Users] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_UserRoles_Users_UserId1] FOREIGN KEY ([UserId1]) REFERENCES [Users] ([Id]) ON DELETE NO ACTION
);
Manual workaround:
builder.Entity<Role>()
.HasMany(e => e.Claims)
.WithOne()
.HasForeignKey(e => e.RoleId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<Role>()
.HasMany(e => e.Users)
.WithOne()
.HasForeignKey(e => e.RoleId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<User>()
.HasMany(e => e.Claims)
.WithOne()
.HasForeignKey(e => e.UserId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<User>()
.HasMany(e => e.Logins)
.WithOne()
.HasForeignKey(e => e.UserId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<User>()
.HasMany(e => e.Roles)
.WithOne()
.HasForeignKey(e => e.UserId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
Further technical details
EF Core version: 2.0 Database Provider: Microsoft.EntityFrameworkCore.SqlServer Operating system: Visual Studio 2017
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:22 (7 by maintainers)
Top Results From Across the Web
Duplicate foreign keys when renaming ASP.NET Identity ...
1. One Foreign Key one Primary Key what is the issue? · 1. the problem here is that the UserId field is the...
Read more >Changing Foreign Keys and Navigations - EF Core
The easiest way to change the relationship between two entities is by manipulating a navigation, while leaving EF Core to fixup the inverse ......
Read more >Relationships, navigation properties, and foreign keys - EF6
In a one-to-one relationship, the primary key acts additionally as a foreign key and there is no separate foreign key column for either...
Read more >dotConnect for PostgreSQL History
The detailed history of all changes and bugfixes made in dotConnect for PostgreSQL.
Read more >Upgrading to ASP.NET Core 2.0 - Code with Steve
With this completed I was able to save and close the project file, which triggers a package restore. Our project file went from...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
My the problem was solved after the transfer base.OnModelCreating(modelBuilder); at beginning of method override OnModelCreating
@adnan-kamili what I suggested was actually not just to comment the code but to reference your new navigations in the
WithOne()
calls. Did you try that?The empty
WithOnes()
are otherwise specifying that those those navigation properties do not represent that relationship, and hence EF Core must assume they represent a separate relationship.