Beispiel #1
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //IDatabaseInitializer<BddContext> init = new CreateDatabaseIfNotExists<BddContext>();
            //IDatabaseInitializer<BddContext> init = new DropCreateDatabaseAlways<BddContext>();
            IDatabaseInitializer <BddContext> init = new DropCreateDatabaseIfModelChanges <BddContext>();

            Database.SetInitializer(init);
            init.InitializeDatabase(new BddContext());
        }
        public ActionResult Index()
        {
            dbInit.InitializeDatabase(db);

            IdentityInitializer i = new IdentityInitializer();

            i.Init(db);

            return(RedirectToAction("Index", "Home"));
        }
        public bool Init(ApplicationDbContext db, string adminUserEmail, string adminPassword)
        {
            try
            {
                dbInit.InitializeDatabase(db);

                var userManager = System.Web.HttpContext.Current.GetOwinContext().GetUserManager <ApplicationUserManager>();

                var roleStore   = new RoleStore <IdentityRole>(db);
                var roleManager = new RoleManager <IdentityRole>(roleStore);

                string name     = adminUserEmail;
                string password = adminPassword;
                string roleName = "Admin";

                //Create Role Admin if it does not exist
                var role = roleManager.FindByName(roleName);
                if (role == null)
                {
                    role = new IdentityRole(roleName);
                    var roleresult = roleManager.Create(role);
                }

                var user = userManager.FindByName(name);
                if (user == null)
                {
                    user = new ApplicationUser {
                        UserName = name, Email = name
                    };
                    var result = userManager.Create(user, password);
                    result = userManager.SetLockoutEnabled(user.Id, false);
                }

                // Add user admin to Role Admin if not already added
                var rolesForUser = userManager.GetRoles(user.Id);
                if (!rolesForUser.Contains(role.Name))
                {
                    var result = userManager.AddToRole(user.Id, role.Name);
                }
                return(true);
            }
#pragma warning disable CS0168 // Variable is declared but never used
            catch (Exception ex)
#pragma warning restore CS0168 // Variable is declared but never used
            {
                return(false);
            }
        }
        public bool Init(ApplicationDbContext db, string adminUserEmail, string adminPassword)
        {
            try
            {
                dbInit.InitializeDatabase(db);

                ApplicationUserManager userManager = System.Web.HttpContext.Current.GetOwinContext().GetUserManager <ApplicationUserManager>();

                RoleStore <IdentityRole>   roleStore   = new RoleStore <IdentityRole>(db);
                RoleManager <IdentityRole> roleManager = new RoleManager <IdentityRole>(roleStore);

                string name     = adminUserEmail;
                string password = adminPassword;
                string roleName = "Admin";

                //Create Role Admin if it does not exist
                IdentityRole role = roleManager.FindByName(roleName);
                if (role == null)
                {
                    role = new IdentityRole(roleName);
                    IdentityResult roleresult = roleManager.Create(role);
                }

                ApplicationUser user = userManager.FindByName(name);
                if (user == null)
                {
                    user = new ApplicationUser {
                        UserName = name, Email = name
                    };
                    IdentityResult result = userManager.Create(user, password);
                    result = userManager.SetLockoutEnabled(user.Id, false);
                }

                // Add user admin to Role Admin if not already added
                System.Collections.Generic.IList <string> rolesForUser = userManager.GetRoles(user.Id);
                if (!rolesForUser.Contains(role.Name))
                {
                    IdentityResult result = userManager.AddToRole(user.Id, role.Name);
                }
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Beispiel #5
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            //IDatabaseInitializer<EasyGradeManagerContext> init = new DropCreateDatabaseAlways<EasyGradeManagerContext>();
            IDatabaseInitializer <EasyGradeManagerContext> init = new DropCreateDatabaseIfModelChanges <EasyGradeManagerContext>();

            Database.SetInitializer(init);
            init.InitializeDatabase(new EasyGradeManagerContext());
            HttpConfiguration config = GlobalConfiguration.Configuration;

            config.Formatters.JsonFormatter
            .SerializerSettings
            .ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
            config.Formatters.JsonFormatter
            .SerializerSettings
            .DateFormatString = "yyyy-MM-dd";
        }
Beispiel #6
0
        public void Configuration(IAppBuilder app)
        {
            try
            {
                // check if db needs to be deleted
                var initializer = new DropCreateDatabaseIfModelChanges <SsnDbContext>();
                Database.SetInitializer(initializer);
                initializer.InitializeDatabase(new SsnDbContext());
            }
            catch (Exception ex)
            {
                Global.Logger.Error(ex.ToString());
            }


            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath          = new PathString("/Authentication/Login")
            });
        }
 public void InitializeDb()
 {
     var initializer = new DropCreateDatabaseIfModelChanges<SurveyorContext>();
       using (var db = new SurveyorContext())
       {
     initializer.InitializeDatabase(db);
     DbSeeder.Seed(db);
     db.SaveChanges();
       }
 }
Beispiel #8
0
        protected void Application_Start(object sender, EventArgs e)
        {
            var dbInitializer = new DropCreateDatabaseIfModelChanges <FrequentFlyersContext>();

            dbInitializer.InitializeDatabase(new FrequentFlyersContext(BlueYonder.FrequentFlyerService.Implementation.FrequentFlyerService.ConnectionName));
        }
Beispiel #9
0
        static void Main(string[] args)
        {
            var dbInitializer = new DropCreateDatabaseIfModelChanges <MyDbContext>();

            dbInitializer.InitializeDatabase(new MyDbContext());

            UserDetailsImpl imp = new UserDetailsImpl();
            UserDetailsDAO  dao = new UserDetailsImpl();


            int    i;
            string ch = "y";



            while (!ch.Equals("n"))
            {
                Console.WriteLine("======================\n1. Add User \n2. Delete User  \n3. Update User \n4. Get All User \n5. Get Data by ID \n======================");
                Console.Write("\nEnter your Choice :  ");
                i = Int32.Parse(Console.ReadLine());

                if (i < 6)
                {
                    switch (i)
                    {
                    case 1:
                        UserDetails u = new UserDetails();
                        Console.Write("\nEnter Name : ");
                        u.Name = Console.ReadLine();
                        Console.Write("Enter Age : ");
                        u.Age = Int32.Parse(Console.ReadLine());

                        Console.Write("\nEnter Email : ");
                        u.UserEmail = Console.ReadLine();

                        Console.Write("\nEnter Password : "******"\nEnter Mobile : ");
                        u.UserMobile = long.Parse(Console.ReadLine());

                        Console.Write("\nEnter Adress : ");
                        u.UserAdress = Console.ReadLine();

                        Console.Write("\nEnter Role : ");
                        u.UserRole = Console.ReadLine();

                        Console.Write("\nEnter Status : ");
                        u.UserStatus = Console.ReadLine();


                        dao.addUser(u);

                        break;

                    case 2:
                        string delChoice = "y";
                        while (!delChoice.Equals("n"))
                        {
                            Console.Write("\nEnter ID to Delete: ");
                            UserDetails ud = new UserDetails();
                            ud.UserId1 = Int32.Parse(Console.ReadLine());
                            ud         = dao.getSqlDataByUserId(ud);
                            Console.WriteLine(ud.UserId1 + "\t" + ud.Name + "\t" + ud.Age + "\t" + ud.UserAdress + "\t" + ud.UserEmail + "\t" + ud.UserPassword + "\t" + ud.UserMobile + "\t" + ud.UserRole + "\t" + ud.UserStatus);
                            Console.Write("Do you wanna delete this record? (y/n) : ");
                            string mchoice = Console.ReadLine();

                            if (mchoice == "y" || mchoice == "Y")
                            {
                                dao.removeUser(ud);
                                Console.WriteLine("Data deleted successfully.");
                                Console.Write("Delete more? (y/n) : ");

                                delChoice = Console.ReadLine();
                            }

                            else if (mchoice == "n" || mchoice == "N")
                            {
                                continue;
                            }

                            else
                            {
                                Console.WriteLine("Invalid input");
                            }
                        }



                        break;

                    case 3:
                        UserDetails uu = new UserDetails();
                        Console.Write("Enter the User ID to update data : ");
                        uu.UserId1 = Int32.Parse(Console.ReadLine());

                        Console.Write("New Name : ");
                        uu.Name = Console.ReadLine();

                        //Console.Write("New ID : ");
                        // uu.UserId1 = Int32.Parse(Console.ReadLine());

                        Console.Write("New Age : ");
                        uu.Age = Int32.Parse(Console.ReadLine());

                        Console.Write("New Adress : ");
                        uu.UserAdress = Console.ReadLine();

                        Console.Write("New Email : ");
                        uu.UserEmail = Console.ReadLine();

                        Console.Write("New Password : "******"\nNew Mobile : ");
                        uu.UserMobile = long.Parse(Console.ReadLine());

                        Console.Write("New Role : ");
                        uu.UserRole = Console.ReadLine();

                        Console.Write("New Status : ");
                        uu.UserStatus = Console.ReadLine();

                        dao.updateUser(uu);

                        break;

                    case 4:
                        List <UserDetails> uall = dao.getAllUsers();
                        Console.WriteLine("\n\nID\tName\tAge\n===================");

                        foreach (UserDetails ur in uall)
                        {
                            Console.WriteLine(ur.UserId1 + "\t" + ur.Name + "\t" + ur.Age + "\t" + ur.UserAdress + "\t" + ur.UserEmail + "\t" + ur.UserPassword + "\t" + ur.UserMobile + "\t" + ur.UserRole + "\t" + ur.UserStatus);
                            Console.WriteLine("-------------------");
                        }
                        break;

                    case 5:
                        UserDetails x = new UserDetails();
                        UserDetails y = new UserDetails();

                        Console.WriteLine("1. Get data by ID \n2. Get data by User ID");
                        int choice = Int32.Parse(Console.ReadLine());

                        if (choice == 1)
                        {
                            Console.Write("Enter ID: ");
                            x.Id = Int32.Parse(Console.ReadLine());
                            y    = dao.getSqlDataById(x);
                            Console.WriteLine(y.UserId1 + "\t" + y.Name + "\t" + y.Age + "\t" + y.UserAdress + "\t" + y.UserEmail + "\t" + y.UserPassword + "\t" + y.UserMobile + "\t" + y.UserRole + "\t" + y.UserStatus);
                        }

                        else if (choice == 2)
                        {
                            Console.WriteLine("Enter User ID : ");
                            x.UserId1 = Int32.Parse(Console.ReadLine());
                            y         = dao.getSqlDataByUserId(x);
                            Console.WriteLine(y.UserId1 + "\t" + y.Name + "\t" + y.Age + "\t" + y.UserAdress + "\t" + y.UserEmail + "\t" + y.UserPassword + "\t" + y.UserMobile + "\t" + y.UserRole + "\t" + y.UserStatus);
                        }

                        else
                        {
                            Console.WriteLine("Invalid Option.....\nPlease try again.");
                            continue;
                        }


                        break;
                    }
                }

                else
                {
                    Console.WriteLine("\nInvalid Option.....\nPlease try again.\n");
                    continue;
                }


                Console.Write("\n\nRun prrogramme again? (y/n) : ");
                ch = Console.ReadLine();

                Console.WriteLine("\n\n");
            }
        }