Entity Framework Core foreign key problem with IdentityUser










0















I am trying to create a new entry in my database with EF Core like this:



RefreshToken newDBRefreshToken = new RefreshToken()

Id = 0,
Token = refreshToken,
AccessToken = accessToken,
UserId = userId
;

_dbContext.RefreshToken.Add(newDBRefreshToken);


I get this error message upon saving:




The foreign key 'UserId: 5b82dbb0-c38b-4144-b51d-e092ef6f5e55' set
on 'RefreshToken' with the key value 'Id: -2147482647' matches an
entity of type 'IdentityUser', however the principal entity type
should be assignable to 'ApplicationUser'.




I have two Entities:



//To make it possible to use AspNetUsers as a reference table
public class ApplicationUser : IdentityUser



public class RefreshToken : BaseEntity //Has int Id and CreateDate ect...

//Reference the IdentityUser
public string UserId get; set;
public ApplicationUser User get; set;

public string Token get; set;
public string AccessToken get; set;
public DateTime ValidUntil get; set;



the migration created my RefreshToken table like this:
enter image description here



Edit:
This is my Startup.cs section:



services.AddDbContext<ApiContext>(options =>
options.UseSqlServer(connection, builder => builder.MigrationsAssembly(typeof(Startup).Assembly.FullName)
).EnableSensitiveDataLogging(true));

// ===== Add Identity ========
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApiContext>()
.AddErrorDescriber<IdentityErrorDescriberDE>()
.AddDefaultTokenProviders();


I tried using ApplicationUser as TUser, which gives me this error:




InvalidOperationException: Unable to resolve service for type
'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]'
while attempting to activate 'Web.Controllers.AccountController'.




This is my Context:



public class ApiContext : IdentityDbContext 
...










share|improve this question



















  • 2





    It's impossible to say without being able to see your context and/or startup, but in one of the two you've referenced IdentityUser as the TUser type param to something that takes that. IdentityDbContext has versions that take generic type params to specify implementations of domain types. You need to make sure it's using your entity, i.e. IdentityDbContext<ApplicationUser> not IdentityDbDontext<IdentityUser>. Also, in startup, you should have something like AddDefaultIdentity<ApplicationUser>(), not AddDefaultIdentity<IdentityUser>().

    – Chris Pratt
    Nov 13 '18 at 14:22











  • Hi, I edited my post. Didn't work. AddDefaultIdentity is not available. What am I missing? :/

    – DoubleVoid
    Nov 13 '18 at 14:32






  • 1





    You need to change your _Login partial, which injects UserManager<IdentityUser> and SignInManager<IdentityUser> by default. Change both of those instances to ApplicationUser as well.

    – Chris Pratt
    Nov 13 '18 at 14:46











  • Hey thanks. Yeah you're right! It's working now :)

    – DoubleVoid
    Nov 13 '18 at 14:58






  • 2





    Just FYI: what was actually happening was because you had IdentityUser as the TUser type param, it was making that the base user entity, and then since ApplicationUser inherited from that, it was setting up a single table inheritance structure with a discriminator column. You were creating users as IdentityUser instances and then attempting associate them as ApplicationUser instances, which they were not. The changes you made caused ApplicationUser to be the user, so the association works fine then.

    – Chris Pratt
    Nov 13 '18 at 16:05















0















I am trying to create a new entry in my database with EF Core like this:



RefreshToken newDBRefreshToken = new RefreshToken()

Id = 0,
Token = refreshToken,
AccessToken = accessToken,
UserId = userId
;

_dbContext.RefreshToken.Add(newDBRefreshToken);


I get this error message upon saving:




The foreign key 'UserId: 5b82dbb0-c38b-4144-b51d-e092ef6f5e55' set
on 'RefreshToken' with the key value 'Id: -2147482647' matches an
entity of type 'IdentityUser', however the principal entity type
should be assignable to 'ApplicationUser'.




I have two Entities:



//To make it possible to use AspNetUsers as a reference table
public class ApplicationUser : IdentityUser



public class RefreshToken : BaseEntity //Has int Id and CreateDate ect...

//Reference the IdentityUser
public string UserId get; set;
public ApplicationUser User get; set;

public string Token get; set;
public string AccessToken get; set;
public DateTime ValidUntil get; set;



the migration created my RefreshToken table like this:
enter image description here



Edit:
This is my Startup.cs section:



services.AddDbContext<ApiContext>(options =>
options.UseSqlServer(connection, builder => builder.MigrationsAssembly(typeof(Startup).Assembly.FullName)
).EnableSensitiveDataLogging(true));

// ===== Add Identity ========
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApiContext>()
.AddErrorDescriber<IdentityErrorDescriberDE>()
.AddDefaultTokenProviders();


I tried using ApplicationUser as TUser, which gives me this error:




InvalidOperationException: Unable to resolve service for type
'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]'
while attempting to activate 'Web.Controllers.AccountController'.




This is my Context:



public class ApiContext : IdentityDbContext 
...










share|improve this question



















  • 2





    It's impossible to say without being able to see your context and/or startup, but in one of the two you've referenced IdentityUser as the TUser type param to something that takes that. IdentityDbContext has versions that take generic type params to specify implementations of domain types. You need to make sure it's using your entity, i.e. IdentityDbContext<ApplicationUser> not IdentityDbDontext<IdentityUser>. Also, in startup, you should have something like AddDefaultIdentity<ApplicationUser>(), not AddDefaultIdentity<IdentityUser>().

    – Chris Pratt
    Nov 13 '18 at 14:22











  • Hi, I edited my post. Didn't work. AddDefaultIdentity is not available. What am I missing? :/

    – DoubleVoid
    Nov 13 '18 at 14:32






  • 1





    You need to change your _Login partial, which injects UserManager<IdentityUser> and SignInManager<IdentityUser> by default. Change both of those instances to ApplicationUser as well.

    – Chris Pratt
    Nov 13 '18 at 14:46











  • Hey thanks. Yeah you're right! It's working now :)

    – DoubleVoid
    Nov 13 '18 at 14:58






  • 2





    Just FYI: what was actually happening was because you had IdentityUser as the TUser type param, it was making that the base user entity, and then since ApplicationUser inherited from that, it was setting up a single table inheritance structure with a discriminator column. You were creating users as IdentityUser instances and then attempting associate them as ApplicationUser instances, which they were not. The changes you made caused ApplicationUser to be the user, so the association works fine then.

    – Chris Pratt
    Nov 13 '18 at 16:05













0












0








0








I am trying to create a new entry in my database with EF Core like this:



RefreshToken newDBRefreshToken = new RefreshToken()

Id = 0,
Token = refreshToken,
AccessToken = accessToken,
UserId = userId
;

_dbContext.RefreshToken.Add(newDBRefreshToken);


I get this error message upon saving:




The foreign key 'UserId: 5b82dbb0-c38b-4144-b51d-e092ef6f5e55' set
on 'RefreshToken' with the key value 'Id: -2147482647' matches an
entity of type 'IdentityUser', however the principal entity type
should be assignable to 'ApplicationUser'.




I have two Entities:



//To make it possible to use AspNetUsers as a reference table
public class ApplicationUser : IdentityUser



public class RefreshToken : BaseEntity //Has int Id and CreateDate ect...

//Reference the IdentityUser
public string UserId get; set;
public ApplicationUser User get; set;

public string Token get; set;
public string AccessToken get; set;
public DateTime ValidUntil get; set;



the migration created my RefreshToken table like this:
enter image description here



Edit:
This is my Startup.cs section:



services.AddDbContext<ApiContext>(options =>
options.UseSqlServer(connection, builder => builder.MigrationsAssembly(typeof(Startup).Assembly.FullName)
).EnableSensitiveDataLogging(true));

// ===== Add Identity ========
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApiContext>()
.AddErrorDescriber<IdentityErrorDescriberDE>()
.AddDefaultTokenProviders();


I tried using ApplicationUser as TUser, which gives me this error:




InvalidOperationException: Unable to resolve service for type
'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]'
while attempting to activate 'Web.Controllers.AccountController'.




This is my Context:



public class ApiContext : IdentityDbContext 
...










share|improve this question
















I am trying to create a new entry in my database with EF Core like this:



RefreshToken newDBRefreshToken = new RefreshToken()

Id = 0,
Token = refreshToken,
AccessToken = accessToken,
UserId = userId
;

_dbContext.RefreshToken.Add(newDBRefreshToken);


I get this error message upon saving:




The foreign key 'UserId: 5b82dbb0-c38b-4144-b51d-e092ef6f5e55' set
on 'RefreshToken' with the key value 'Id: -2147482647' matches an
entity of type 'IdentityUser', however the principal entity type
should be assignable to 'ApplicationUser'.




I have two Entities:



//To make it possible to use AspNetUsers as a reference table
public class ApplicationUser : IdentityUser



public class RefreshToken : BaseEntity //Has int Id and CreateDate ect...

//Reference the IdentityUser
public string UserId get; set;
public ApplicationUser User get; set;

public string Token get; set;
public string AccessToken get; set;
public DateTime ValidUntil get; set;



the migration created my RefreshToken table like this:
enter image description here



Edit:
This is my Startup.cs section:



services.AddDbContext<ApiContext>(options =>
options.UseSqlServer(connection, builder => builder.MigrationsAssembly(typeof(Startup).Assembly.FullName)
).EnableSensitiveDataLogging(true));

// ===== Add Identity ========
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApiContext>()
.AddErrorDescriber<IdentityErrorDescriberDE>()
.AddDefaultTokenProviders();


I tried using ApplicationUser as TUser, which gives me this error:




InvalidOperationException: Unable to resolve service for type
'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]'
while attempting to activate 'Web.Controllers.AccountController'.




This is my Context:



public class ApiContext : IdentityDbContext 
...







c# asp.net-core asp.net-identity entity-framework-core






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 14:32







DoubleVoid

















asked Nov 13 '18 at 14:12









DoubleVoidDoubleVoid

368629




368629







  • 2





    It's impossible to say without being able to see your context and/or startup, but in one of the two you've referenced IdentityUser as the TUser type param to something that takes that. IdentityDbContext has versions that take generic type params to specify implementations of domain types. You need to make sure it's using your entity, i.e. IdentityDbContext<ApplicationUser> not IdentityDbDontext<IdentityUser>. Also, in startup, you should have something like AddDefaultIdentity<ApplicationUser>(), not AddDefaultIdentity<IdentityUser>().

    – Chris Pratt
    Nov 13 '18 at 14:22











  • Hi, I edited my post. Didn't work. AddDefaultIdentity is not available. What am I missing? :/

    – DoubleVoid
    Nov 13 '18 at 14:32






  • 1





    You need to change your _Login partial, which injects UserManager<IdentityUser> and SignInManager<IdentityUser> by default. Change both of those instances to ApplicationUser as well.

    – Chris Pratt
    Nov 13 '18 at 14:46











  • Hey thanks. Yeah you're right! It's working now :)

    – DoubleVoid
    Nov 13 '18 at 14:58






  • 2





    Just FYI: what was actually happening was because you had IdentityUser as the TUser type param, it was making that the base user entity, and then since ApplicationUser inherited from that, it was setting up a single table inheritance structure with a discriminator column. You were creating users as IdentityUser instances and then attempting associate them as ApplicationUser instances, which they were not. The changes you made caused ApplicationUser to be the user, so the association works fine then.

    – Chris Pratt
    Nov 13 '18 at 16:05












  • 2





    It's impossible to say without being able to see your context and/or startup, but in one of the two you've referenced IdentityUser as the TUser type param to something that takes that. IdentityDbContext has versions that take generic type params to specify implementations of domain types. You need to make sure it's using your entity, i.e. IdentityDbContext<ApplicationUser> not IdentityDbDontext<IdentityUser>. Also, in startup, you should have something like AddDefaultIdentity<ApplicationUser>(), not AddDefaultIdentity<IdentityUser>().

    – Chris Pratt
    Nov 13 '18 at 14:22











  • Hi, I edited my post. Didn't work. AddDefaultIdentity is not available. What am I missing? :/

    – DoubleVoid
    Nov 13 '18 at 14:32






  • 1





    You need to change your _Login partial, which injects UserManager<IdentityUser> and SignInManager<IdentityUser> by default. Change both of those instances to ApplicationUser as well.

    – Chris Pratt
    Nov 13 '18 at 14:46











  • Hey thanks. Yeah you're right! It's working now :)

    – DoubleVoid
    Nov 13 '18 at 14:58






  • 2





    Just FYI: what was actually happening was because you had IdentityUser as the TUser type param, it was making that the base user entity, and then since ApplicationUser inherited from that, it was setting up a single table inheritance structure with a discriminator column. You were creating users as IdentityUser instances and then attempting associate them as ApplicationUser instances, which they were not. The changes you made caused ApplicationUser to be the user, so the association works fine then.

    – Chris Pratt
    Nov 13 '18 at 16:05







2




2





It's impossible to say without being able to see your context and/or startup, but in one of the two you've referenced IdentityUser as the TUser type param to something that takes that. IdentityDbContext has versions that take generic type params to specify implementations of domain types. You need to make sure it's using your entity, i.e. IdentityDbContext<ApplicationUser> not IdentityDbDontext<IdentityUser>. Also, in startup, you should have something like AddDefaultIdentity<ApplicationUser>(), not AddDefaultIdentity<IdentityUser>().

– Chris Pratt
Nov 13 '18 at 14:22





It's impossible to say without being able to see your context and/or startup, but in one of the two you've referenced IdentityUser as the TUser type param to something that takes that. IdentityDbContext has versions that take generic type params to specify implementations of domain types. You need to make sure it's using your entity, i.e. IdentityDbContext<ApplicationUser> not IdentityDbDontext<IdentityUser>. Also, in startup, you should have something like AddDefaultIdentity<ApplicationUser>(), not AddDefaultIdentity<IdentityUser>().

– Chris Pratt
Nov 13 '18 at 14:22













Hi, I edited my post. Didn't work. AddDefaultIdentity is not available. What am I missing? :/

– DoubleVoid
Nov 13 '18 at 14:32





Hi, I edited my post. Didn't work. AddDefaultIdentity is not available. What am I missing? :/

– DoubleVoid
Nov 13 '18 at 14:32




1




1





You need to change your _Login partial, which injects UserManager<IdentityUser> and SignInManager<IdentityUser> by default. Change both of those instances to ApplicationUser as well.

– Chris Pratt
Nov 13 '18 at 14:46





You need to change your _Login partial, which injects UserManager<IdentityUser> and SignInManager<IdentityUser> by default. Change both of those instances to ApplicationUser as well.

– Chris Pratt
Nov 13 '18 at 14:46













Hey thanks. Yeah you're right! It's working now :)

– DoubleVoid
Nov 13 '18 at 14:58





Hey thanks. Yeah you're right! It's working now :)

– DoubleVoid
Nov 13 '18 at 14:58




2




2





Just FYI: what was actually happening was because you had IdentityUser as the TUser type param, it was making that the base user entity, and then since ApplicationUser inherited from that, it was setting up a single table inheritance structure with a discriminator column. You were creating users as IdentityUser instances and then attempting associate them as ApplicationUser instances, which they were not. The changes you made caused ApplicationUser to be the user, so the association works fine then.

– Chris Pratt
Nov 13 '18 at 16:05





Just FYI: what was actually happening was because you had IdentityUser as the TUser type param, it was making that the base user entity, and then since ApplicationUser inherited from that, it was setting up a single table inheritance structure with a discriminator column. You were creating users as IdentityUser instances and then attempting associate them as ApplicationUser instances, which they were not. The changes you made caused ApplicationUser to be the user, so the association works fine then.

– Chris Pratt
Nov 13 '18 at 16:05












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282932%2fentity-framework-core-foreign-key-problem-with-identityuser%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282932%2fentity-framework-core-foreign-key-problem-with-identityuser%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto