我正在使用代码优先迁移在ASP.NET中开发Web应用程序.它在本地工作正常,但在部署到Azure后,代码首次迁移不会执行.我已经一步一步地跟踪
this tutorial几次,但我无法发现我的设置有什么问题.这是相关的代码:
DB上下文:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection",throwIfV1Schema: false) {} public DbSet<BC_Instance> BiocloudInstances { get; set; } static ApplicationDbContext() {} public static ApplicationDbContext Create() { return new ApplicationDbContext(); } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); var conv = new AttributeToTableAnnotationConvention<SoftDeleteAttribute,string>( "SoftDeleteColumnName",(type,attributes) => attributes.Single().ColumnName); modelBuilder.Conventions.Add(conv); } }
连接字符串:
(它在发布时被替换,但以防万一)
<add name="DefaultConnection" connectionString="Data Source=.\sqlEXPRESS;Initial Catalog=bcplatform2;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.sqlClient" /></connectionStrings>
代码优先迁移配置
internal sealed class Configuration : DbMigrationsConfiguration<bcplatform2.Models.ApplicationDbContext> { public Configuration() { AutomaticMigrationsEnabled = false; } protected override void Seed(bcplatform2.Models.ApplicationDbContext context) { var userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(context)); var roleManager = new ApplicationRoleManager(new RoleStore<ApplicationRole>(context)); const string name = {name here}; const string password = {pass here}; const string adminRole = {role}; string[] roles = new string[] { adminRole,ApplicationRole.DefaultRoleName }; foreach (string role in roles) { if (!context.Roles.Any(r => r.Name == role)) { roleManager.CreateAsync(new ApplicationRole(role)); } } if (!context.Users.Any(u => u.UserName == name)) { var user = new ApplicationUser { UserName = name,Email = name,credit = 10 }; userManager.Create(user,password); userManager.AddToRole(user.Id,adminRole); userManager.SetLockoutEnabled(user.Id,false); } } }
部署的Web.config中的实体框架部分
<entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory,EntityFramework"> <parameters> <parameter value="mssqllocaldb" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.sqlClient" type="System.Data.Entity.sqlServer.sqlProviderServices,EntityFramework.sqlServer" /> </providers> <contexts> <context type="bcplatform2.Models.ApplicationDbContext,bcplatform2"> <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[bcplatform2.Models.ApplicationDbContext,bcplatform2],[bcplatform2.Migrations.Configuration,bcplatform2]],EntityFramework,PublicKeyToken={token}"> <parameters> <parameter value="DefaultConnection_DatabasePublish" /> </parameters> </databaseInitializer> </context> </contexts> </entityFramework>
已部署的Web.config中的连接字符串
<connectionStrings> <add name="DefaultConnection" connectionString="Data Source=tcp:{serverid}.database.windows.net,1433;Initial Catalog={dbid};User Id={user};Password={password}" providerName="System.Data.sqlClient" /> <add name="DefaultConnection_DatabasePublish" connectionString="Data Source=tcp:{serverid}.database.windows.net,1433;Initial Catalog={dbid};User ID={user};Password={password}" providerName="System.Data.sqlClient" /> </connectionStrings>
解决方法
您可以在解决方案中更新Web.config文件
没有必要在“context”部分中提供连接字符串,因为您已经在ApplicationDbContext构造函数中提供了它.
此外,使用此配置,您可以取消选中“发布向导”中的“执行代码首次迁移”.
您的EF部分应该与此类似(最重要的是“上下文”部分):
<entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory,EntityFramework"> <parameters> <parameter value="mssqllocaldb" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.sqlClient" type="System.Data.Entity.sqlServer.sqlProviderServices,EntityFramework.sqlServer" /> </providers> <contexts> <context type="TestWebApp.Models.AppContext,TestWebApp"> <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[TestWebApp.Models.AppContext,TestWebApp],[TestWebApp.Migrations.Configuration,TestWebApp]],EntityFramework" /> </context> </contexts>