public static void StartInitializer()
        {
            Database.SetInitializer<EFDbContext>(null);

            try
            {
                using (var context = new EFDbContext())
                {
                    if (!context.Database.Exists())
                    {
                        ((IObjectContextAdapter) context).ObjectContext.CreateDatabase();
                    }
                }

                WebSecurity.InitializeDatabaseConnection("DatabaseConnection", "UserProfiles", "UserId", "Email",
                                                         autoCreateTables: true);
                WebSecurityExtensions.InitializeDatabaseConnection("DatabaseConnection");

                foreach (string role in SiteRoles.Roles.Where(role => Roles.RoleExists(role) == false))
                {
                    Roles.CreateRole(role);
                }

                if (WebSecurity.UserExists(SiteSettings.FirstUserName) == false)
                {
                    WebSecurity.CreateUserAndAccount(SiteSettings.FirstUserName, SiteSettings.FirstUserPassword);
                    Roles.AddUserToRole(SiteSettings.FirstUserName, SiteRoles.Admins);
                }
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(
                    "Не удалось инициализировать базу данных ASP.NET Simple Membership. " +
                    "Чтобы получить дополнительные сведения, перейдите по адресу: http://go.microsoft.com/fwlink/?LinkId=256588",
                    ex);
            }
        }
示例#2
0
 //public ManagerRepository():this(db)
 //{
 //    db = new EFDbContext();
 //    //ManagerRepository(db);
 //}
 public ManagerRepository()
 {
     this.db = GetDbContext();
 }
示例#3
0
        private static int EFOption(string EmployeeName, out int NewRowID)
        {
            //Set up the variables
            int intRC = 0; //Used to trap the Stored Procedure's return code

            //Create a Connection object using Entity Framework (EF)
            IObjectContextAdapter Context = new EFDbContext();

            //Set up the parameters using my custom parameter factory
            IParameterFactory objParams = new EmployeesParameterFactory(EmployeeName: EmployeeName);
            //-- Change the RC parameter from ReturnValue to Output, since the EF does not support ReturnValue (Crazy!)
            objParams.Parmeters["RC"].Direction = ParameterDirection.Output;

            //Create and configure an ADO.NET command object 
            //-- Create a SQL command string
            string strSQLCode = @"Exec @RC = pInsEmployees" +
                                " @EmployeeName = '" + objParams.Parmeters["EmployeeName"] + "'" + // Don't forget the SINGLE Quotes!!!
                                ",@NewRowID = @NewRowID out;";
            //configure the command object and execute it
            try
            {
                Context.ObjectContext.ExecuteStoreCommand(strSQLCode
                                                            , objParams.Parmeters["RC"]
                                                            , objParams.Parmeters["EmployeeName"]
                                                            , objParams.Parmeters["NewRowID"]
                                                            );

                //Get the new row ID created by the table's Identity feature
                if (objParams.Parmeters["NewRowID"].Value is DBNull)
                { NewRowID = 0; } //if the insert has failed, then set this to an arbitrary number
                else { NewRowID = (int)objParams.Parmeters["NewRowID"].Value; } //else send it back as output

                //Trap or return the Stored Procedure's return code
                intRC = (int)objParams.Parmeters["RC"].Value;
                if (intRC < 0)
                { throw new Exception("Error reported in Stored Procedure: " + objParams.Parmeters["RC"].Value.ToString()); }
            }
            catch (Exception)
            {

                throw;
            }

            return intRC;
        }