我使用的n层体系结构为我的Web应用程序和的DbContext类是这里ASP.NET MVC N层的Code First迁移
public class Db:DbContext
{
public Db()
: base("DbConnectionString")
{
Database.SetInitializer<Db>(null);
}
public class DbInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<Db>
{
protected override void Seed(Db context)
{
}
}
public DbSet<User> Users { get; set; }
public DbSet<Role> Roles { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>().HasMany(r => r.Roles).WithMany(o => o.Users).Map(f =>
{
f.MapLeftKey("UserId");
f.MapRightKey("RoleId");
});
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
我的app.config文件在这里
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DbConnectionString" connectionString="Server=xx.xxx.xx.xxx;Database=gonulluh_first;User Id=*******;Password=*******;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<contexts>
<context type="Data.Db, Data">
<databaseInitializer type="DbInitializer, Data" />
</context>
</contexts>
<defaultConnectionFactory type="Data.DbContextFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
当我在包管理器控制台中使用“update-database -verbose”命令时,它会将我的数据库迁移到本地数据库。
Using StartUp project 'Web'.
Using NuGet project 'Data'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'DbConnectionString' (DataSource: (localdb)\v11.0, Provider: System.Data.SqlClient, Origin: Convention).
No pending explicit migrations.
Running Seed method.
你知道我为什么不能更新远程数据库吗?