我有一个启用并配置为将我的数据库与一些额外的表/字段一起使用的ASP.NET-MVC
应用程序。Migration
SimpleMemberShipProvider
我的问题是,将我的数据库初始化放在哪里?因为我仍在开发它,所以我可以删除数据库/表和数据并重新创建它。
我已将它放在 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)
{
}
}
}
}