C
C#10mo ago
SoulsReaper

How to connect my already constructed MySQL database to my Blazor app as an Identity DB Context!?.

hello guys, I need help to connect my MySQL database to a new blazor app as an identity db context, I followed a lot of tutorials but all fails, I am new to db migration , tried it and it just created new tables to my database, I already has the database and tables, I just want my blazor app to connect to mysql database and use the data in it for signup/sign in, how to activate the register/login options and connect my database to my app? my database already works as an Identity db context on another .NET framwork app with no issues.
40 Replies
Brainiac V
Brainiac V10mo ago
Is this a Blazor Server app or WASM?
SoulsReaper
SoulsReaper10mo ago
Server app, I am creating an Admin dashboard for the database, db already workign as an Identity db context for the users' app using .net framework, the web app is quite old, I fix bugs, clean code and add features, but I have no idea how the devs before me even added the db as identity.
Brainiac V
Brainiac V10mo ago
Well for connecting the database you need to provide the connection string somewhere and then you have to build the pages and functionality to login and register
Brainiac V
Brainiac V10mo ago
Medium
Blazor Server Project #12: Installing ASP.NET Core Identity
An initial guide to security: installing Identity and adding its database to the existing project
SoulsReaper
SoulsReaper10mo ago
is it possible to do that manually with .Net 6 and blazor? I see a lot of built in functions/interfaces used in the web app that base on using add-migration / update-database commands and UserManager Class. Also when I tried using migration it just added new empty tables to my database, glad I am using a dev environment 😅
Brainiac V
Brainiac V10mo ago
migrations and updating the database has nothing to do with Identity itself or logging users in it's just the way you manage db changes with code first
SoulsReaper
SoulsReaper10mo ago
so I dont need to migrate/update db ? just using dbContext and assign it as Identity from base code? I can skip the migration part?
Brainiac V
Brainiac V10mo ago
Well you need to migrate and update if you change your entities in the code and of course you need somewhere a migration that adds the tables for Identity
SoulsReaper
SoulsReaper10mo ago
what if I already has the tables for identity? old app using them, cant I just use the same tables /database for a different app?
Brainiac V
Brainiac V10mo ago
that depends if you want those users to be able to access your app or not But then you probably also have two databases, one for your application data, the other one for the user information
SoulsReaper
SoulsReaper10mo ago
I will totally give this a read and try to apply, thanks 🙂
Brainiac V
Brainiac V10mo ago
also a problem can be, if someone extended the user information there or roles and you don't want or need that, you have a problem because you can't change it just for your app so I would be very careful with this approach just using the existing database
SoulsReaper
SoulsReaper10mo ago
so, I need another whole database and after creating an Identity I just connect the other db for example to manage users data from admin side etc?
Brainiac V
Brainiac V10mo ago
What other db?
SoulsReaper
SoulsReaper10mo ago
for admin dashbord
Brainiac V
Brainiac V10mo ago
If you create a new one, your application data and Identity should be stored in the same db
SoulsReaper
SoulsReaper10mo ago
just for identity/ login yes exactly, they are suppose to be in same db, no need for new one, then whats the uses of migration? sorry but I am totally new to this concept
Brainiac V
Brainiac V10mo ago
I don't know what you're talking about you are confusing so many things
SoulsReaper
SoulsReaper10mo ago
okay my bad, I will try and clarify slowly
Brainiac V
Brainiac V10mo ago
Also MySQL as DB is a pretty bad choice
SoulsReaper
SoulsReaper10mo ago
Well, I cant change the DB since its not my choice, I am just a developer in a company that uses it would have been much much easier if they used sql server since its built in with blazor and visual studio
Brainiac V
Brainiac V10mo ago
That wouldn't change anything in your scenario
SoulsReaper
SoulsReaper10mo ago
yeah I agree
Brainiac V
Brainiac V10mo ago
you still need to provide a connection string to tell the application where the DB is located, the DB itself is not that important for that
SoulsReaper
SoulsReaper10mo ago
I can do that, but how to make it as identity? shall I manually code the claims,users,roles etc related to identity dB context?
Brainiac V
Brainiac V10mo ago
I don't think you understand how Identity works at all When you add Identity to your project correctly and then create the migration and update the database you have tables for users, roles already
SoulsReaper
SoulsReaper10mo ago
well not totally, I know that It's for authorization and authentication
Brainiac V
Brainiac V10mo ago
and then you can decide if you need to extend them or not
SoulsReaper
SoulsReaper10mo ago
yes I have them already, both tables exists
Brainiac V
Brainiac V10mo ago
so then also the connection string is in place, otherwise they couldn't have been created
SoulsReaper
SoulsReaper10mo ago
but when I added the connectionstring to my databtase on program.cs and followed the tutorial a=I was watching, used add-migration then update-database, it totally added new tables for example: aspcoreusers aspcoreroles
Brainiac V
Brainiac V10mo ago
I don't know which tutorial you watched and what exactly you did there
SoulsReaper
SoulsReaper10mo ago
ZetBit
YouTube
How to use Identity in ASP.NET Core with MySQL (Part 1 of 2)
In this video i will show you how to use identity in ASP.NET Core and how to set it up with MySQL instead of MSSQL. Identity is a powerful framework to use designed by Microsoft for allowing users to be added to your website. Download Project: https://zetbit.tech/Downloads/Project/1/Identity-Project-Part-1-and-2 00:00 Introduction 01:24 Scaffol...
SoulsReaper
SoulsReaper10mo ago
this is the tutorial I followed
Brainiac V
Brainiac V10mo ago
But this isn't even a Blazor Server Project But yeah the Identity parts shouldn't be much different, you scaffold them and then apply the migration to your database after that you can start building pages e.g. for login, etc. or use the existing ones and customize them that's on you and your requirements But Identity pretty much gives you everything you need for the basics
SoulsReaper
SoulsReaper10mo ago
yeah since I am using asp .NET 6 , it shouldnt be any different or atl east not much difference btw the link you provided is locked behind upgrading to premium, I cant read it.
Brainiac V
Brainiac V10mo ago
Just using the incognito mode should make it work
SoulsReaper
SoulsReaper10mo ago
I cant in both ways, wbsite wants me to login, after logging in wants me to pay to read the full article
SoulsReaper
SoulsReaper10mo ago
Accord
Accord10mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.