2013-05-14 68 views
3

我有一個ASP.NET-MVC應用程序啓用MigrationSimpleMemberShipProvider配置爲使用我的數據庫與一些額外的表/字段。數據庫初始化策略代碼第一

我的問題是,在哪裏把我的數據庫初始化?因爲我仍然在開發它,所以我可以放棄數據庫/表和數據並重新創建它。

我已經把它放在configuration.cs文件中,但我不確定這是否是數據庫初始值設定項的地方。

namespace CFContext.Migrations 
{ 
    using System; 
    using System.Data.Entity; 
    using System.Data.Entity.Migrations; 
    using System.Linq; 
    using WebMatrix.WebData; 
    using System.Web.Security; 
    using System.Collections.Generic; 

internal sealed class Configuration : DbMigrationsConfiguration<DataContext> 
{ 
    public Configuration() 
    { 
     Database.SetInitializer(new DropCreateDatabaseAlways<DataContext>()); 
     //Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DataContext>()); 
     AutomaticMigrationsEnabled = true; 
     AutomaticMigrationDataLossAllowed = true; 
    } 

    protected override void Seed(DataContext context) 
    { 
     // This method will be called after migrating to the latest version. 

     // You can use the DbSet<T>.AddOrUpdate() helper extension method 
     // to avoid creating duplicate seed data. E.g. 
     // 
     // context.People.AddOrUpdate(
     //  p => p.FullName, 
     //  new Person { FullName = "Andrew Peters" }, 
     //  new Person { FullName = "Brice Lambson" }, 
     //  new Person { FullName = "Rowan Miller" } 
     // ); 
     // 

     SeedMembership(context); 
    } 

    private void SeedMembership(DataContext context) 
    { 
     WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); 

     var roles = (SimpleRoleProvider)Roles.Provider; 
     var membership = (SimpleMembershipProvider)System.Web.Security.Membership.Provider; 

     if (!roles.RoleExists("Administrator")) 
      roles.CreateRole("Administrator"); 

     if (membership.GetUser("Username0", false) == null) 
     { 

     } 
     } 
    } 
} 

回答

6
public class ContextInitializer : CreateDatabaseIfNotExists<Context> 
{  
    private static void InitializeWebSecurity() 
    { 
     if (WebSecurity.Initialized) 
      return; 

     WebSecurity.InitializeDatabaseConnection(
      connectionStringName: "DefaultConnection", 
      userTableName: "User", 
      userIdColumn: "Id", 
      userNameColumn: "Email", 
      autoCreateTables: true); 

     Roles.CreateRole("Admin"); 
     Roles.CreateRole("Customer"); 
    }  

    protected override void Seed(Context context) 
    { 
     InitializeWebSecurity(); 

     // more seeding 
     context.SaveChanges(); 
    } 
} 

public class MvcApplication : System.Web.HttpApplication 
{ 
    protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 
     WebApiConfig.Register(GlobalConfiguration.Configuration); 
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
     RouteConfig.RegisterRoutes(RouteTable.Routes); 
     BundleConfig.RegisterBundles(BundleTable.Bundles); 
     AuthConfig.RegisterAuth(); 
     DatabaseConfig.RegisterDatabase(); 
     AutoMapperConfig.RegisterConfig(); 

     // Set the initializer here 
     Database.SetInitializer(new ContextInitializer()); 

     // Now initialize it 
     using (var context = new Context()) 
     { 
      context.Database.Initialize(false); 
     } 

     // Double check seeding has initalized, if not 
     // We initialize it here to make sure. 
     if (!WebSecurity.Initialized) 
     { 
      WebSecurity.InitializeDatabaseConnection(
       connectionStringName: "DefaultConnection", 
       userTableName: "User", 
       userIdColumn: "Id", 
       userNameColumn: "Email", 
       autoCreateTables: false); 
     } 
    } 
} 

你應該能夠創建遷移與此設置相同的方式。

+0

嗨,這裏令人困惑的部分是,如果你啓用遷移,configuration.cs文件已經有一個保護覆蓋無效種子(上下文上下文)方法。可以在多個地方使用嗎? – Yustme 2013-05-14 14:20:36

+0

你應該只設置一個初始化策略,所以CreateDatabaseIfNotExists 不應該在你的。只要確保你設置了初始化策略來使用你的Migrations設置。有兩種不同的類型不應該影響它。 – 2013-05-14 15:10:23

+0

我的意思是隻有Seed()方法,它在啓用遷移時也位於configuration.cs中。我會有Seed()方法兩次。 – Yustme 2013-05-14 16:14:00

5

把你的數據庫的初始化代碼爲 「的Application_Start」 方法上Global.asax中

+0

嗨,如果我在NuGet控制檯執行'更新數據庫'命令會發生什麼?它會在Global.asax中查看嗎? – Yustme 2013-05-14 12:31:39

+1

它應該知道一旦你編譯程序集,需要發生什麼。 – 2013-05-14 13:27:55

+0

我在類庫中啓用了遷移。我將數據庫操作從Web項目中分離出來。 – Yustme 2013-05-14 14:21:56

0

我把它放在裏面的Application_Start在Global.asax.cs中

protected void Application_Start() 
     { 
      WebSecurity.InitializeDatabaseConnection("Name of Connection String", "User Table in DataBase", "UserId Column", "UserName Column", true); 
     } 

真,意味着我要自動創建的表。

在Web.config文件中,你需要設置供應商:

<system.web> 
    <compilation debug="true" targetFramework="4.6.1" /> 
    <httpRuntime targetFramework="4.6.1" /> 

    <membership defaultProvider="nameProvider"> 
     <providers> 
     <add name="nameProvider" type="WebMatrix.WebData.SimpleMembershipProvider,WebMatrix.WebData" /> 
     </providers> 
    </membership> 
    </system.web> 

這對我的作品。