Hack Forums
[C#] [.net core] Initialize DB if empty? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] [.net core] Initialize DB if empty? (/showthread.php?tid=5814340)



[.net core] Initialize DB if empty? - severos - Mar 21, 2018

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??


RE: [.net core] Initialize DB if empty? - iNCEPTiON_V - Mar 21, 2018

We need more Infotmation than just "DB". Are you using EntityFramework.Core.SQlite ? with Repositorys?
Tell us your complete Environment.


RE: [.net core] Initialize DB if empty? - severos - Mar 22, 2018

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.


RE: [.net core] Initialize DB if empty? - theogg - Mar 22, 2018

(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?


RE: [.net core] Initialize DB if empty? - severos - Mar 22, 2018

(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();
 
       } 



RE: [.net core] Initialize DB if empty? - Ninja - Mar 23, 2018

Why would you need a default user if the database is empty?


RE: [.net core] Initialize DB if empty? - severos - Mar 23, 2018

(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.


RE: [.net core] Initialize DB if empty? - Ninja - Mar 23, 2018

(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.