C
C#•3mo 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•3mo ago
Have you created and ran the migrations?
Lamp
Lamp•3mo 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
Lamp•3mo ago
No description
Lamp
Lamp•3mo ago
oops turns out i hadnt
Pobiega
Pobiega•3mo ago
:when:
Lamp
Lamp•3mo ago
how do i get it to work tho like the migration error
Pobiega
Pobiega•3mo ago
No description
Pobiega
Pobiega•3mo ago
You tell me.
Lamp
Lamp•3mo ago
fuck enchantment table errors one moment omg it wont let me copy the text
Lamp
Lamp•3mo ago
No description
Lamp
Lamp•3mo ago
is that any better
Pobiega
Pobiega•3mo ago
Well it's readable, but none of those are errors
Lamp
Lamp•3mo ago
wait so why is there an X
Pobiega
Pobiega•3mo ago
Is that all the text? Also... Ef power tools? Wtf is that
Lamp
Lamp•3mo 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
Lamp•3mo 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•3mo ago
thats distinctly not true, as I've never heard of it before :p dotnet ef is all you need
Lamp
Lamp•3mo ago
ok
Pobiega
Pobiega•3mo 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
Lamp•3mo 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•3mo ago
Ah right, its the preview thingy
Lamp
Lamp•3mo 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•3mo 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
Lamp•3mo 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•3mo ago
Why are you uploading text? Just paste it.
Lamp
Lamp•3mo ago
i did it auto did that
Pobiega
Pobiega•3mo ago
If it's too long, use pastebin Opening txt files on a phone is not a fun experience
leowest
leowest•3mo ago
because of the cahracter size
Lamp
Lamp•3mo 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•3mo ago
BlazeBin - bhtelnoscqgm
A tool for sharing your source code with the world!
Lamp
Lamp•3mo ago
lol sorry about that
leowest
leowest•3mo ago
its not your fault, its just that its too long so discord automatically converts it to a file
Lamp
Lamp•3mo ago
yea i hate it when it does that XD
leowest
leowest•3mo ago
I blame Pobiega :madcar:
Pobiega
Pobiega•3mo ago
Wtf Ok I'm at a bathhouse so Leo, congratulations I appoint you in charge of this
Lamp
Lamp•3mo ago
ok anyway so clearly my models are fucked if theres all these shadow properties right
leowest
leowest•3mo ago
well I dont know Identity so lets see @Lamp where u deploying these to? local db or what
Lamp
Lamp•3mo ago
sql server
leowest
leowest•3mo 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•3mo ago
Those errors are from generating the migration, no? It sure look like it
Lamp
Lamp•3mo ago
ahh, it is kinda valuable unfortunately
Pobiega
Pobiega•3mo ago
Are you adding identity to an existing database?
leowest
leowest•3mo ago
and u should have scaffolded the db models not create a new one on top
Lamp
Lamp•3mo 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
Lamp•3mo ago
and i believe the db started out with identity, like all the AspNetUsers, AspNetRoles, etc are there
No description
Jimmacle
Jimmacle•3mo 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
Lamp•3mo 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
Lamp•3mo ago
ig so far its been "db-first" design?
Jimmacle
Jimmacle•3mo 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
Lamp•3mo ago
now that you mention it, that makes a lot of sense
Jimmacle
Jimmacle•3mo ago
why are you trying to scaffold an identity database that another application generated anyway?
Lamp
Lamp•3mo ago
its a relatively new project so its .net8 but where would i check the identity version? i assume its the newest?
Jimmacle
Jimmacle•3mo ago
you'd check the code of the application that created this db
Lamp
Lamp•3mo 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•3mo 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
Lamp•3mo ago
yes, the project im working on is the same one that owns the db. im like 99% sure of that
Jimmacle
Jimmacle•3mo ago
then you shouldn't be scaffolding anything or making any of your own migrations unless you change the models yourself
Lamp
Lamp•3mo ago
wait, why?
Jimmacle
Jimmacle•3mo ago
because the application will already have the migrations it used to get the database to the state it's in now
Lamp
Lamp•3mo ago
sooo... about that. there are none
Jimmacle
Jimmacle•3mo ago
okay, go to the database and look at the __EFMigrationsHistory table
Lamp
Lamp•3mo ago
its empty and didnt exist (i dont remember seeing it) until i tried to run my own migration
Jimmacle
Jimmacle•3mo ago
does the application call .EnsureCreated() anywhere in its startup code or something?
Lamp
Lamp•3mo ago
no
Jimmacle
Jimmacle•3mo ago
so how did the database come to exist in the first place?
Lamp
Lamp•3mo ago
I assume my boss made it somehow
Jimmacle
Jimmacle•3mo 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
Lamp•3mo 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•3mo ago
that would be very dumb and unlikely, but technically maybe
Lamp
Lamp•3mo ago
hmm is there any way for me to fix it?
leowest
leowest•3mo ago
that would still make no sense thou he made the app right?
Lamp
Lamp•3mo ago
yes
Jimmacle
Jimmacle•3mo 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•3mo ago
so he should have the models right
Jimmacle
Jimmacle•3mo 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
Lamp•3mo 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•3mo 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
Lamp•3mo ago
ok i just did the initial commit
Jimmacle
Jimmacle•3mo ago
add a row that looks something like this
No description
Jimmacle
Jimmacle•3mo ago
but with your actual migration name
Lamp
Lamp•3mo ago
i was just going to ask that, lol, thank you!
Jimmacle
Jimmacle•3mo ago
assuming the models match the DB then you'll be good, if not you're kind of stuck
Lamp
Lamp•3mo ago
the name of the file is 20240307155516_InitialCreate.cs, do i need to include the "create" portion?
Jimmacle
Jimmacle•3mo ago
yeah just take the .cs off
Lamp
Lamp•3mo ago
No description
Lamp
Lamp•3mo ago
good?
Jimmacle
Jimmacle•3mo 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
Lamp•3mo ago
how would i test that it works?
Jimmacle
Jimmacle•3mo ago
this doesn't do anything on its own, it just stops future migrations from trying to create things that already exist
Lamp
Lamp•3mo ago
ok gotcha
Jimmacle
Jimmacle•3mo 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
Lamp•3mo 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•3mo 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
Lamp•3mo ago
sadly yes i 100% fucked up
Jimmacle
Jimmacle•3mo 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
Lamp•3mo ago
a fresh copy of the repo? i assume?
Jimmacle
Jimmacle•3mo 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
Lamp•3mo 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•3mo ago
as in the deployed version doesn't work either?
Lamp
Lamp•3mo 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•3mo 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
Lamp•3mo ago
fair
Jimmacle
Jimmacle•3mo 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
Lamp•3mo ago
wait wdym how would i do that
Jimmacle
Jimmacle•3mo 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
Lamp•3mo ago
ohh i see before i do that, i need my app to work tho
Jimmacle
Jimmacle•3mo ago
this is part of making your app work
Lamp
Lamp•3mo ago
cause it wont open now
Jimmacle
Jimmacle•3mo ago
that's why i'm saying start over and re-apply your code but without the stuff that broke it
Lamp
Lamp•3mo ago
the only thing that i changed from when it was running to when it broke was updating to .net8.0.2
Jimmacle
Jimmacle•3mo ago
(also fwiw you can install specific versions of tools and not update your whole project) then undo that
Lamp
Lamp•3mo 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•3mo ago
then install the .NET version that worked
Lamp
Lamp•3mo ago
Ok, literally restarted my pc and its working again lmfao Love that