-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Adding a foriegn key using ApplicationUser
(Identity) fails
#7288
Comments
@HaoK can you help with a repro here? |
@Biarity the UserId in IdentityUserLogin is the foreign key for the ApplicationUser, the key for the UserLogin is defined as: |
FYI, I've also run into this exact problem. I am also using EF Core 1.1 and Npgsql/postgres, but working on a Mac with VS Code. I will try to create a repro to demonstrate the issue. |
Repro of issue available at https://github.com/tbaggett/EFIssue7288Repro. I followed these steps to create the project:
Note that this example is using Sqlite as implemented by default. Using PostgreSQL appears to be unrelated to the problem. Full error output: bash-3.2$ dotnet ef migrations add AddedTestContext --context TestContext |
Unassigning so triage can pick this up now that there's a repro |
@HaoK can you isolate whether there is an EF bug here or not. |
I am also having the same issue I am using Ubuntu 16.04 LTS, VS Code, Core 1.1 and Postgresql (Npsql) I have two contexts: LibrilleIdentityContext and LibrilleContext. However, when I run
The line is
|
What's the difference between your two context's @smithaitufe |
@HaoK Thanks for reaching out LibrilleIdentityContext.cs
and my LibrilleContext.cs
They are both using the same connection string. |
Try moving your base.OnModelCreating call up, see: // Add your customizations after calling base.OnModelCreating(builder); |
@HaoK |
Perhaps the issue is the fact that there are two DbContexts? Is there any way you can try things temporarily with a single db context just to see if that might be causing issues? |
I did and I did not have any issue. |
So this issue only occurs with two different DbContext types? @ajcvickers @divega are there any limitations today with using migrations and multiple db contexts? |
Okay, so can I close this as dupe #2725 since this error doesn't occur if only a single DbContext is involved |
Yes, because TestModel references ApplicationUser, thereby creating two overlapping models. |
Was this problem solved? |
I want to add a foreign key to one of my business models (a
DbSet
) in order to associate it with a user.To do that I added the following code to my model (using
ApplicationUser
as discovery):ApplicationUser
is the ASP.NET Core Identity user model that's created by default in VS when you select individual user authentication for a project.Running
Update-Database
gives the following error:The entity type 'IdentityUserLogin<string>' requires a primary key to be defined.
. However, looking at the mentioned class, I see it has aUserId
property, which should be considered a primary key (also the generatedAspNetUsers
table has an Id field). To make it clearer that it has a primary key, I added the following code toApplicationDbContext.OnModelCreating
:builder.Entity<ApplicationUser>().HasKey(u => u.Id);
. I still get the same error.Notes
I am using Npgsql & a remote psotgres database. I also have IdentityServer 4 working with the Identity system.
Further technical details
EF Core version: 1.1.0
Database Provider: Npgsql.EntityFrameworkCore.PostgreSQL
Operating system: Windows
IDE: same issue in VS2015 & VS2017 RC2
The text was updated successfully, but these errors were encountered: