1、依次添加NuGet包 EntityFrameworkNpgsqlEntityFramework6.Npgsql,会自动生成一些配置文件,不过缺少数据库驱动的配置节点:

   <system.data>
<DbProviderFactories>
<!-- 注意这里,安装程序包时,这里的配置并不会自动添加 -->
<remove invariant="Npgsql" />
<add name="Npgsql" invariant="Npgsql" description=".Net Framework Data Provider for Postgresql" type="Npgsql.NpgsqlFactory, Npgsql" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<!-- 数据库连接字符串, 主机, 用户, 密码, 数据库 -->
<add name="mes" connectionString="Server=localhost;Uid=postgres;Password=1234;Database=Mes" providerName="Npgsql"/>
</connectionStrings>

2、添加测试的数据库模型:

     public class BaseModel
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set; }
public string Remark { get; set; }
public DateTime CreTime { get; set; } = DateTime.Now;
public bool IsDel { get; set; } = false;
public DateTime? UpdateTime { get; set; }
} [Table("User")]
public class User : BaseModel
{
public string Account { get; set; }
public string Label { get; set; }
public string Passwd { get; set; }
public long? UpdateUid { get; set; }
} [Table("Order")]
public class Order: BaseModel
{
public string OrderNo { get; set; }
public decimal Qty { get; set; }
public string Style { get; set; }
public string Size { get; set; }
public string Color { get; set; }
public DateTime? DeliveryTime { get; set; }
public string ShipAddress { get; set; }
public string Phone { get; set; }
public long? CustomerId { get; set; } [ForeignKey("CustomerId")]
public User Customer { get; set; }
}

3、数据库上下文:

     public class MesContext:DbContext
{
public MesContext():base("name=mes")
{ } public DbSet<User> Users { get; set; }
public DbSet<Order> Orders { get; set; }
}

4、添加自动迁移配置:

     public class MigrationConfig:DbMigrationsConfiguration<MesContext>
{
public MigrationConfig()
{
AutomaticMigrationsEnabled = true;
//到生产环境后,注释以下
AutomaticMigrationDataLossAllowed = true;
}
}

5、发布代码时,自动迁移到最新:

             using (var db = new MesContext())
{
new MigrateDatabaseToLatestVersion<MesContext, MigrationConfig>("mes").InitializeDatabase(db); //测试添加User
db.Users.Add(new User() {Account = "", Label = "shanghai", Passwd = ""});
db.SaveChanges();
}

6、完整配置文件: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>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
</providers>
</entityFramework>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Npgsql" publicKeyToken="5d8b90d52f46fda7" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.2.5.0" newVersion="3.2.5.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.data>
<DbProviderFactories>
<!-- 注意这里,安装程序包时,这里的配置并不会自动添加 -->
<remove invariant="Npgsql" />
<add name="Npgsql" invariant="Npgsql" description=".Net Framework Data Provider for Postgresql" type="Npgsql.NpgsqlFactory, Npgsql" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<!-- 数据库连接字符串, 主机, 用户, 密码, 数据库 -->
<add name="mes" connectionString="Server=localhost;Uid=postgres;Password=1234;Database=Mes" providerName="Npgsql"/>
</connectionStrings>
</configuration>

7、NuGet配置:packages.config

 <?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.2.0" targetFramework="net45" />
<package id="EntityFramework6.Npgsql" version="3.1.1" targetFramework="net45" />
<package id="Npgsql" version="3.2.5" targetFramework="net45" />
<package id="System.Threading.Tasks.Extensions" version="4.3.0" targetFramework="net45" />
</packages>

注:第一次自动迁移前需要手动先创建数据库(Mes)

最新文章

  1. Java生成XML文件
  2. Java中if语句,嵌套使用 一道面试题
  3. PHP_Memcache函数详解
  4. Hadoop.2.x_简单的测试文件读取与上传
  5. OGLplus 0.33.0 发布,OpenGL 的 C 封装库
  6. tomcat监控脚本
  7. JavaScript学习笔记(十二) 回调模式(Callback Pattern)
  8. Android View绘制13问13答
  9. mac下firefox复制粘贴失效解决办法
  10. JavaScript高级程序设计16.pdf
  11. Linux命令之exit
  12. Swift中文教程(六)--枚举和结构
  13. Linux安装php的Redis扩展
  14. 201521123106《java程序设计》第四周学习总结
  15. ROS 可视化(一): 发布PointCloud2点云数据到Rviz
  16. OneNET麒麟座应用开发之六:与气体质量流量控制器通讯
  17. supervisor //todo
  18. 开启SSI配置使shtml支持include公用的页头页脚
  19. 2018/03/11 每日一个Linux命令 之 top
  20. Python3 文件操作(十六)

热门文章

  1. Visual Studio 2008 SP1键盘F10单步调试超慢解决方法
  2. 浅谈K8S cni和网络方案
  3. 《Beginning Java 7》 - 5 - Hash Codes 哈希码
  4. 求解任意图的最小支配集(Minimun Dominating Set)
  5. jquery源码解析:代码结构分析
  6. mysql数据的导入导出
  7. windows系统作为客户端时,linux中本地yum源挂载时,如何同时挂载DVD1和DVD2?
  8. Django上传文件的那些参数
  9. rest-assured之如何指定请求数据(Specifying Request Data 包括请求参数、请求头、cookie等)
  10. CDQZ Day6