我启用了代码优先迁移。这是我的configuration.cs:
public sealed class Configuration : DbMigrationsConfiguration<App.AppDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(AppDbContext context)
{
SqlConnection.ClearAllPools();
context.Database.CreateIfNotExists();
System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<AppDbContext, Configuration>());
if (!WebMatrix.WebData.WebSecurity.Initialized)
{
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
}
SeedMembership(context);
}
使用SeedMembership:
private int SeedMembership(DbContext context)
{
var roles = (SimpleRoleProvider)Roles.Provider;
var membership = (SimpleMembershipProvider)Membership.Provider;
if (!roles.RoleExists("Administrator"))
{
roles.CreateRole("Administrator");
}
if (!roles.RoleExists("User"))
{
roles.CreateRole("User");
}
if (!roles.RoleExists("Moderator"))
{
roles.CreateRole("Moderator");
}
if (membership.GetUser("admin", false) == null)
{
membership.CreateUserAndAccount("admin", "covert");
}
if (!Roles.IsUserInRole("admin", "Administrator"))
{
roles.AddUsersToRoles(new[] { "admin" }, new[] { "Administrator" });
}
context.SaveChanges();
return membership.GetUserId("admin");
}
我的web.config中只定义了一个连接字符串:
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=.\;Initial Catalog=App;Trusted_Connection=True;MultipleActiveResultSets=True;" />
如果我在本地运行update-database,我会在包管理器控制台中看到“运行种子方法”,并且所有内容都按照我的预期进行播种。
然而,当我在我的azure数据库上更新数据库时(不确定azure在这里是否相关),我看到“运行种子方法”,但数据库永远不会使用roles / admin用户进行更新。其他表已创建。
我整晚都在这上面,不知道还有什么要看的。以下是我用于迁移azure db的内容:
更新 - 数据库-Verbose -StartUpProjectName“App” -ConnectionString“Server = tcp:app.database.windows.net,1433; Database = App; User ID = Dbadmin @应用;密码=密码; Trusted_Connection = FALSE;加密= TRUE;连接 超时= 30; MultipleActiveResultSets = True;最大池大小= 100;“ -ConnectionProviderName“System.Data.SqlClient”
为什么这不起作用?