Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

[.net core] Initialize DB if empty?

I have a .net core 2 web application (MVC) with entity framework and using the IdentityUser.
I want to check using the injected UserManager if the current user DB is empty or not, if it's empty then I need to initialize it with a default user.
What's the proper place to initialize? and how would I implement it properly??
Have a cool signature? hit me up and I might use it.
We need more Infotmation than just "DB". Are you using EntityFramework.Core.SQlite ? with Repositorys?
Tell us your complete Environment.
I'm using EF and connecting to LocalSQL.
What I currently do is write an Init controller that will initialize DB values for me, after that I delete it from project. However I find it pretty ugly way of doing it, so I'm looking for a better way of initializing DB.
Have a cool signature? hit me up and I might use it.
(Mar 22, 2018 04:15 AM)severos Wrote:  I'm using EF and connecting to LocalSQL.
What I currently do is write an Init controller that will initialize DB values for me, after that I delete it from project. However I find it pretty ugly way of doing it, so I'm looking for a better way of initializing DB.

Can you post code, please?
(Mar 22, 2018 03:44 PM)theogg Wrote:  Can you post code, please?

Can't see why you'd need the code... anyway here it is:
PHP Code

<?php 
void InitDb
(UserManager<ApplicationUser> userManager, RoleManager<ApplicationRole> roleManager)
 
       {
 
           var admin = new ApplicationUser
            
{
 
               UserName = "Admin",
 
               EmailConfirmed = true,
 
               PhoneNumberConfirmed = true,
 
               Id = 0
            
};
 
           userManager.CreateAsync(admin, "AdminDef@1t").Wait();
 
           var adminRole = new ApplicationRole{Description = "Super Admin",Name = "Super Admin"};
 
           roleManager.CreateAsync(adminRole).Wait();
 
           var adminPanelRole = new ApplicationRole {Description = "Access to Admin Panel", Name = "Admin Panel"};
 
           roleManager.CreateAsync(adminPanelRole).Wait();
 
           var createContent =
 
               new ApplicationRole {Description = "Able to submit news/jobs", Name = "Content Creator"};
 
           roleManager.CreateAsync(createContent).Wait();
 
           userManager.AddToRolesAsync(admin, new string[] {"Super Admin", "Admin Panel", "Content Creator"}).Wait();
 
       } 
Have a cool signature? hit me up and I might use it.
Why would you need a default user if the database is empty?
(Mar 23, 2018 05:20 AM)Ninja Wrote:  Why would you need a default user if the database is empty?

So the admin can start working?
Normally I would enter admin login manually, however there are some concurrency stamps and other fields that I don't like to manually enter, so I wanted an automatic way for doing it.
Have a cool signature? hit me up and I might use it.
(Mar 23, 2018 05:26 AM)severos Wrote:  So the admin can start working?
Normally I would enter admin login manually, however there are some concurrency stamps and other fields that I don't like to manually enter, so I wanted an automatic way for doing it.

This could be solved using a stored procedure. I dont know how that works with EF as I have little experience with it, though.


Possibly Related Threads...
Thread / Author Replies Views Last Post
by Neo_KaiserAug 10, 2025 02:36 PM 0 110 Aug 10, 2025 02:36 PM
Last Post: Neo_Kaiser
[C] by maryousJul 24, 2022 03:35 PM 0 161 Jul 24, 2022 03:35 PM
Last Post: maryous
[.NET] by JohnsonTheKnownedSep 13, 2021 10:34 AM 5 141 Sep 13, 2021 10:34 AM
Last Post: Scapula™