C
C#•9mo ago
Lamp

ASP.NET Identity Login Error

When trying to sign in using SignInManager, I get a SqlException: Invalid column name 'UserId1'. Why is there an extra user column being added to the query? My extended identity classes are listed below.
var result = await _signInManager.PasswordSignInAsync(
user,
Input.Password,
Input.RememberMe,
lockoutOnFailure: false
);
var result = await _signInManager.PasswordSignInAsync(
user,
Input.Password,
Input.RememberMe,
lockoutOnFailure: false
);
public class ApplicationUser : IdentityUser
{
[Required(ErrorMessage = "First name is required")]
[Display(Name = "First Name")]
[MaxLength(50)]
public string FirstName { get; set; }

[Required(ErrorMessage = "Last name is required")]
[Display(Name = "Last Name")]
[MaxLength(50)]
public string LastName { get; set; }

public virtual ICollection<ApplicationUserClaim> Claims { get; set; }

public virtual ICollection<IdentityUserLogin<string>> Logins { get; set; }

public virtual ICollection<IdentityUserToken<string>> Tokens { get; set; }
}

public class ApplicationRole : IdentityRole
{
}

public class ApplicationUserRole : IdentityUserRole<string>
{
public bool IsReadOnly { get; set; }

public string ProgramId { get; set; }

public bool IsDefault { get; set; }

public virtual TestProgram Program { get; set; }
}

public class ApplicationRoleClaim : IdentityRoleClaim<string>
{
public virtual ApplicationRole Role { get; set; }
}

public class ApplicationUserClaim : IdentityUserClaim<string>
{
public virtual ApplicationUser User { get; set; }
}

public partial class ApplicationDbContext : IdentityDbContext<
ApplicationUser, ApplicationRole, string,
ApplicationUserClaim, ApplicationUserRole, IdentityUserLogin<string>,
ApplicationRoleClaim, IdentityUserToken<string>>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}

/// DbSets

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
public class ApplicationUser : IdentityUser
{
[Required(ErrorMessage = "First name is required")]
[Display(Name = "First Name")]
[MaxLength(50)]
public string FirstName { get; set; }

[Required(ErrorMessage = "Last name is required")]
[Display(Name = "Last Name")]
[MaxLength(50)]
public string LastName { get; set; }

public virtual ICollection<ApplicationUserClaim> Claims { get; set; }

public virtual ICollection<IdentityUserLogin<string>> Logins { get; set; }

public virtual ICollection<IdentityUserToken<string>> Tokens { get; set; }
}

public class ApplicationRole : IdentityRole
{
}

public class ApplicationUserRole : IdentityUserRole<string>
{
public bool IsReadOnly { get; set; }

public string ProgramId { get; set; }

public bool IsDefault { get; set; }

public virtual TestProgram Program { get; set; }
}

public class ApplicationRoleClaim : IdentityRoleClaim<string>
{
public virtual ApplicationRole Role { get; set; }
}

public class ApplicationUserClaim : IdentityUserClaim<string>
{
public virtual ApplicationUser User { get; set; }
}

public partial class ApplicationDbContext : IdentityDbContext<
ApplicationUser, ApplicationRole, string,
ApplicationUserClaim, ApplicationUserRole, IdentityUserLogin<string>,
ApplicationRoleClaim, IdentityUserToken<string>>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}

/// DbSets

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
114 Replies
Pobiega
Pobiega•9mo ago
Have you created and ran the migrations?
Lamp
LampOP•9mo ago
yes i have, my db matches that i mentioned it happening with PasswordSignInAsync() but honestly it keeps happening with almost any method related to those tables
Lamp
LampOP•9mo ago
No description
Lamp
LampOP•9mo ago
oops turns out i hadnt
Pobiega
Pobiega•9mo ago
:when:
Lamp
LampOP•9mo ago
how do i get it to work tho like the migration error
Pobiega
Pobiega•9mo ago
No description
Pobiega
Pobiega•9mo ago
You tell me.
Lamp
LampOP•9mo ago
fuck enchantment table errors one moment omg it wont let me copy the text
Lamp
LampOP•9mo ago
No description
Lamp
LampOP•9mo ago
is that any better
Pobiega
Pobiega•9mo ago
Well it's readable, but none of those are errors
Lamp
LampOP•9mo ago
wait so why is there an X
Pobiega
Pobiega•9mo ago
Is that all the text? Also... Ef power tools? Wtf is that
Lamp
LampOP•9mo ago
GitHub
GitHub - ErikEJ/EFCorePowerTools: Entity Framework Core Power Tools...
Entity Framework Core Power Tools - reverse engineering, migrations and model visualization in Visual Studio & CLI - ErikEJ/EFCorePowerTools
Lamp
LampOP•9mo ago
everyone uses it theres so much i cant fit it on my screen and the window isnt resizeable lmao the migration tool is in preview still ig so i should probably do it via command line?
Pobiega
Pobiega•9mo ago
thats distinctly not true, as I've never heard of it before :p dotnet ef is all you need
Lamp
LampOP•9mo ago
ok
Pobiega
Pobiega•9mo ago
also, uhhh EF7? why does it say EF7 in several places what .NET version are you using on this project? And what packagereference version is your EF package?
Lamp
LampOP•9mo ago
.net 8 also that repo isnt mine i just wanted to give u more info on the tool i was using so ignore the ef7 thing
Pobiega
Pobiega•9mo ago
Ah right, its the preview thingy
Lamp
LampOP•9mo ago
but yea im using .net8 with razor pages Microsoft.Data.SqlClient.SqlException (0x80131904): There is already an object named 'TestTypes' in the database. i tried doing it via cli but it throws an error on my first table saying it already exists?
Jimmacle
Jimmacle•9mo ago
sounds like your migrations are out of sync with your database if there's nothing valuable in the DB just delete it and recreate it
Lamp
LampOP•9mo ago
ok so i reran the migration command but before i could, it wanted me to update to .net8.0.2, so i did, and now i got this in the console and my app wont even open in the browser now lmfaoo
Pobiega
Pobiega•9mo ago
Why are you uploading text? Just paste it.
Lamp
LampOP•9mo ago
i did it auto did that
Pobiega
Pobiega•9mo ago
If it's too long, use pastebin Opening txt files on a phone is not a fun experience
leowest
leowest•9mo ago
because of the cahracter size
Lamp
LampOP•9mo ago
Pastebin
[10:55:14 DBG] An 'IServiceProvider' was created for internal use b...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
leowest
leowest•9mo ago
BlazeBin - bhtelnoscqgm
A tool for sharing your source code with the world!
Lamp
LampOP•9mo ago
lol sorry about that
leowest
leowest•9mo ago
its not your fault, its just that its too long so discord automatically converts it to a file
Lamp
LampOP•9mo ago
yea i hate it when it does that XD
leowest
leowest•9mo ago
I blame Pobiega :madcar:
Pobiega
Pobiega•9mo ago
Wtf Ok I'm at a bathhouse so Leo, congratulations I appoint you in charge of this
Lamp
LampOP•9mo ago
ok anyway so clearly my models are fucked if theres all these shadow properties right
leowest
leowest•9mo ago
well I dont know Identity so lets see @Lamp where u deploying these to? local db or what
Lamp
LampOP•9mo ago
sql server
leowest
leowest•9mo ago
and did u dump the db before running migrations again given what macle said, if u dont have anything of value in it
Pobiega
Pobiega•9mo ago
Those errors are from generating the migration, no? It sure look like it
Lamp
LampOP•9mo ago
ahh, it is kinda valuable unfortunately
Pobiega
Pobiega•9mo ago
Are you adding identity to an existing database?
leowest
leowest•9mo ago
and u should have scaffolded the db models not create a new one on top
Lamp
LampOP•9mo ago
ok so long story short, i got hired recently, boss is in the hospital (2 person team) so i cant ask him, he just handed me a "copy" of the db that i can use locally along with the app he was working on afaik we're using the individual accounts razor pages template just with a bunch of our own stuff built on top
Lamp
LampOP•9mo ago
and i believe the db started out with identity, like all the AspNetUsers, AspNetRoles, etc are there
No description
Jimmacle
Jimmacle•9mo ago
are you sure the version of the code you're using matches what is compatible with the DB you were given? that sounds more like you were given 2 things that didn't work together to begin with
Lamp
LampOP•9mo ago
honestly, no im not entirely sure, however i have used the efcore power tools reverse engineer feature to generate the models and such for me... but when i do that, none of the identity code comes in, i have to manually add that every time
No description
Lamp
LampOP•9mo ago
ig so far its been "db-first" design?
Jimmacle
Jimmacle•9mo ago
what version of identity/ASP.NET generated the database? i don't think db-first is going to work nicely here because the models it would be generating are built into the identity library
Lamp
LampOP•9mo ago
now that you mention it, that makes a lot of sense
Jimmacle
Jimmacle•9mo ago
why are you trying to scaffold an identity database that another application generated anyway?
Lamp
LampOP•9mo ago
its a relatively new project so its .net8 but where would i check the identity version? i assume its the newest?
Jimmacle
Jimmacle•9mo ago
you'd check the code of the application that created this db
Lamp
LampOP•9mo ago
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.2" /> <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.2" /> yea its the latest one i believe
Jimmacle
Jimmacle•9mo ago
is the project you're working on now the same one that "owns" this database or are you trying to pull in data from another application?
Lamp
LampOP•9mo ago
yes, the project im working on is the same one that owns the db. im like 99% sure of that
Jimmacle
Jimmacle•9mo ago
then you shouldn't be scaffolding anything or making any of your own migrations unless you change the models yourself
Lamp
LampOP•9mo ago
wait, why?
Jimmacle
Jimmacle•9mo ago
because the application will already have the migrations it used to get the database to the state it's in now
Lamp
LampOP•9mo ago
sooo... about that. there are none
Jimmacle
Jimmacle•9mo ago
okay, go to the database and look at the __EFMigrationsHistory table
Lamp
LampOP•9mo ago
its empty and didnt exist (i dont remember seeing it) until i tried to run my own migration
Jimmacle
Jimmacle•9mo ago
does the application call .EnsureCreated() anywhere in its startup code or something?
Lamp
LampOP•9mo ago
no
Jimmacle
Jimmacle•9mo ago
so how did the database come to exist in the first place?
Lamp
LampOP•9mo ago
I assume my boss made it somehow
Jimmacle
Jimmacle•9mo ago
yes, the options are either migrations or calling EnsureCreated it should 100% be migrations if you ever planned to change anything about the database if you have no migrations, this was broken before you ever touched it
Lamp
LampOP•9mo ago
wait is it possible he made it manually then just used the reverse engineer tool to generate the models then manually added the identity dbcontext stuff
Jimmacle
Jimmacle•9mo ago
that would be very dumb and unlikely, but technically maybe
Lamp
LampOP•9mo ago
hmm is there any way for me to fix it?
leowest
leowest•9mo ago
that would still make no sense thou he made the app right?
Lamp
LampOP•9mo ago
yes
Jimmacle
Jimmacle•9mo ago
assuming the code you have has the same model that was used to set up the DB, you can create an initial migration and manually record it in the __EFMigrationsHistory table
leowest
leowest•9mo ago
so he should have the models right
Jimmacle
Jimmacle•9mo ago
so EF won't try to apply it and future migrations will build on it or find someone who can answer why there are no migrations in the codebase
Lamp
LampOP•9mo ago
ok i did some searching and in the repo we have, his first commit had migrations, however he deleted the folder in the next commit and those would be pretty out of date for how the tables look now so i should just do this?
Jimmacle
Jimmacle•9mo ago
they should never have been deleted, they're as important as the rest of the code in the project yeah if they're just gone you should create a new "initial" migration and fake it being applied
Lamp
LampOP•9mo ago
ok i just did the initial commit
Jimmacle
Jimmacle•9mo ago
add a row that looks something like this
No description
Jimmacle
Jimmacle•9mo ago
but with your actual migration name
Lamp
LampOP•9mo ago
i was just going to ask that, lol, thank you!
Jimmacle
Jimmacle•9mo ago
assuming the models match the DB then you'll be good, if not you're kind of stuck
Lamp
LampOP•9mo ago
the name of the file is 20240307155516_InitialCreate.cs, do i need to include the "create" portion?
Jimmacle
Jimmacle•9mo ago
yeah just take the .cs off
Lamp
LampOP•9mo ago
No description
Lamp
LampOP•9mo ago
good?
Jimmacle
Jimmacle•9mo ago
yeah, i don't know what it uses the productversion for so you may have to make that a real EF version but you can try it
Lamp
LampOP•9mo ago
how would i test that it works?
Jimmacle
Jimmacle•9mo ago
this doesn't do anything on its own, it just stops future migrations from trying to create things that already exist
Lamp
LampOP•9mo ago
ok gotcha
Jimmacle
Jimmacle•9mo ago
you've basically synced up what the migration tool thinks the DB currently looks like with what it actually currently looks like (assuming your models matched already)
Lamp
LampOP•9mo ago
shit new issue, when i installed the ef cli tools, i had to update to .net 8.0.2, and now the app opens in the browser but cant find any of my pages 😭 i didnt even touch anything i updated from 8.0.1 to 8.0.2
Jimmacle
Jimmacle•9mo ago
do you have important work on your working branch? if not i'd just roll back to whatever you were initially given, do the migration thing, then go from there
Lamp
LampOP•9mo ago
sadly yes i 100% fucked up
Jimmacle
Jimmacle•9mo ago
either way, i'd start fresh and make that work, then move your changes on top of that check out a fresh copy somewhere else to try fixing
Lamp
LampOP•9mo ago
a fresh copy of the repo? i assume?
Jimmacle
Jimmacle•9mo ago
right then theoretically you can apply your existing commits on it once you've fixed it, minus any scaffolding jank or just copy/paste the code
Lamp
LampOP•9mo ago
so with that, there really hasnt been a point where the identity stuff worked i mean maybe when the app was created
Jimmacle
Jimmacle•9mo ago
as in the deployed version doesn't work either?
Lamp
LampOP•9mo ago
this app isnt deployed yet, we're trying to get it ready for production soon for queries, most things work, but if its related to identity, it tends to break that's my main issue i've noticed
Jimmacle
Jimmacle•9mo ago
so just for fun, use your migration to create a brand new DB and see if things work that way assuming your application can bootstrap itself from a fresh database, which isn't a guarantee at this point :KEKW: if nothing is in prod then the existing data probably isn't that important
Lamp
LampOP•9mo ago
fair
Jimmacle
Jimmacle•9mo ago
and that will at least help you determine whether identity is really broken or someone just messed up the database at some point and never fixed it which is likely if they thought deleting migrations was a good idea
Lamp
LampOP•9mo ago
wait wdym how would i do that
Jimmacle
Jimmacle•9mo ago
i mean if the application code depends on data already being in the database then it will probably not work if the database is brand new only one way to find out, create a db, apply the migration, and see if it works
Lamp
LampOP•9mo ago
ohh i see before i do that, i need my app to work tho
Jimmacle
Jimmacle•9mo ago
this is part of making your app work
Lamp
LampOP•9mo ago
cause it wont open now
Jimmacle
Jimmacle•9mo ago
that's why i'm saying start over and re-apply your code but without the stuff that broke it
Lamp
LampOP•9mo ago
the only thing that i changed from when it was running to when it broke was updating to .net8.0.2
Jimmacle
Jimmacle•9mo ago
(also fwiw you can install specific versions of tools and not update your whole project) then undo that
Lamp
LampOP•9mo ago
so i didnt update the project i literally installed it from https://dotnet.microsoft.com/en-us/download/dotnet/8.0 *installed .net 8.0.2 apologies i had a meeting CS9057 The analyzer assembly 'C:\Program Files\dotnet\sdk\8.0.201\Sdks\Microsoft.NET.Sdk.Razor\source-generators\Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll' references version '4.9.0.0' of the compiler, which is newer than the currently running version '4.8.0.0'.
i noticed this in my error list, but just listed as a warning could that be it? i did this but i just cant test it yet cause the app wont open
Jimmacle
Jimmacle•9mo ago
then install the .NET version that worked
Lamp
LampOP•9mo ago
Ok, literally restarted my pc and its working again lmfao Love that
Want results from more Discord servers?
Add your server