我是一名有用的助手,可以为您翻译文本。
在部署的 Web.config 中的连接字符串。
我正在使用ASP.NET进行编码优先迁移开发Web应用程序。在本地工作正常,但部署到Azure后,编码优先迁移未执行。我已经按照此教程多次逐步操作,但我没有能够发现我的设置有什么问题。以下是相关代码:
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>
Code First Migrations配置
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>
<parameter value="mssqllocaldb" />
或<databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion\
2` 这里是否正确。你知道应该是这样吗? - nestDefaultConnection
的内容完全相同的部分。至于删除parameters
部分,我不知道如何操作,因为它是在部署过程中生成的代码,不在VisualStudio项目中。谢谢。 - nest