PostgreSQL Entity Framework 自动迁移
1、依次添加NuGet包 EntityFramework、Npgsql、EntityFramework6.Npgsql,会自动生成一些配置文件,不过缺少数据库驱动的配置节点:
1 <system.data> 2 <DbProviderFactories> 3 <!-- 注意这里,安装程序包时,这里的配置并不会自动添加 --> 4 <remove invariant="Npgsql" /> 5 <add name="Npgsql" invariant="Npgsql" description=".Net Framework Data Provider for Postgresql" type="Npgsql.NpgsqlFactory, Npgsql" /> 6 </DbProviderFactories> 7 </system.data> 8 <connectionStrings> 9 <!-- 数据库连接字符串, 主机, 用户, 密码, 数据库 --> 10 <add name="mes" connectionString="Server=localhost;Uid=postgres;Password=1234;Database=Mes" providerName="Npgsql"/> 11 </connectionStrings>
2、添加测试的数据库模型:
1 public class BaseModel 2 { 3 [Key] 4 [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 5 public long Id { get; set; } 6 public string Remark { get; set; } 7 public DateTime CreTime { get; set; } = DateTime.Now; 8 public bool IsDel { get; set; } = false; 9 public DateTime? UpdateTime { get; set; } 10 } 11 12 [Table("User")] 13 public class User : BaseModel 14 { 15 public string Account { get; set; } 16 public string Label { get; set; } 17 public string Passwd { get; set; } 18 public long? UpdateUid { get; set; } 19 } 20 21 [Table("Order")] 22 public class Order: BaseModel 23 { 24 public string OrderNo { get; set; } 25 public decimal Qty { get; set; } 26 public string Style { get; set; } 27 public string Size { get; set; } 28 public string Color { get; set; } 29 public DateTime? DeliveryTime { get; set; } 30 public string ShipAddress { get; set; } 31 public string Phone { get; set; } 32 public long? CustomerId { get; set; } 33 34 [ForeignKey("CustomerId")] 35 public User Customer { get; set; } 36 }
3、数据库上下文:
1 public class MesContext:DbContext 2 { 3 public MesContext():base("name=mes") 4 { 5 6 } 7 8 9 public DbSet<User> Users { get; set; } 10 public DbSet<Order> Orders { get; set; } 11 }
4、添加自动迁移配置:
1 public class MigrationConfig:DbMigrationsConfiguration<MesContext> 2 { 3 public MigrationConfig() 4 { 5 AutomaticMigrationsEnabled = true; 6 //到生产环境后,注释以下 7 AutomaticMigrationDataLossAllowed = true; 8 } 9 }
5、发布代码时,自动迁移到最新:
1 using (var db = new MesContext()) 2 { 3 new MigrateDatabaseToLatestVersion<MesContext, MigrationConfig>("mes").InitializeDatabase(db); 4 5 //测试添加User 6 db.Users.Add(new User() {Account = "009", Label = "shanghai", Passwd = "123456"}); 7 db.SaveChanges(); 8 }
6、完整配置文件:App.config
1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 <configSections> 4 <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 5 <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 6 </configSections> 7 <startup> 8 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 9 </startup> 10 <entityFramework> 11 <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> 12 <providers> 13 <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 14 <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" /> 15 </providers> 16 </entityFramework> 17 <runtime> 18 <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 19 <dependentAssembly> 20 <assemblyIdentity name="Npgsql" publicKeyToken="5d8b90d52f46fda7" culture="neutral" /> 21 <bindingRedirect oldVersion="0.0.0.0-3.2.5.0" newVersion="3.2.5.0" /> 22 </dependentAssembly> 23 </assemblyBinding> 24 </runtime> 25 <system.data> 26 <DbProviderFactories> 27 <!-- 注意这里,安装程序包时,这里的配置并不会自动添加 --> 28 <remove invariant="Npgsql" /> 29 <add name="Npgsql" invariant="Npgsql" description=".Net Framework Data Provider for Postgresql" type="Npgsql.NpgsqlFactory, Npgsql" /> 30 </DbProviderFactories> 31 </system.data> 32 <connectionStrings> 33 <!-- 数据库连接字符串, 主机, 用户, 密码, 数据库 --> 34 <add name="mes" connectionString="Server=localhost;Uid=postgres;Password=1234;Database=Mes" providerName="Npgsql"/> 35 </connectionStrings> 36 </configuration>
7、NuGet配置:packages.config
1 <?xml version="1.0" encoding="utf-8"?> 2 <packages> 3 <package id="EntityFramework" version="6.2.0" targetFramework="net45" /> 4 <package id="EntityFramework6.Npgsql" version="3.1.1" targetFramework="net45" /> 5 <package id="Npgsql" version="3.2.5" targetFramework="net45" /> 6 <package id="System.Threading.Tasks.Extensions" version="4.3.0" targetFramework="net45" /> 7 </packages>
注:第一次自动迁移前需要手动先创建数据库(Mes)
posted on 2017-11-14 21:47 jonney_wang 阅读(630) 评论(0) 编辑 收藏 举报