PostgreSQL Entity Framework 自动迁移
1、依次添加NuGet包 EntityFramework、Npgsql、EntityFramework6.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)
PostgreSQL Entity Framework 自动迁移的更多相关文章
- entity framework自动迁移
第一步,建立测试项目,普通的WinForm类型,EntityMigration: 第二步,从NuGet为项目添加MySql.Data.Entity,由Oracle提供,我选择人气高的: 第三步,建立实 ...
- 关于Entity Framework自动关联查询与自动关联更新导航属性对应的实体注意事项说明
一.首先了解下Entity Framework 自动关联查询: Entity Framework 自动关联查询,有三种方法:Lazy Loading(延迟加载),Eager Loading(预先加载) ...
- 使用Entity Framework 自动产生的Sql语句
对于一个单独实体的通常操作有3种:添加新的实体.修改实体以及删除实体. 1.添加新的实体 Entity Framework Code First添加新的实体通过调用DbSet.Add()方法来实现. ...
- Entity Framework 自动生成CodeFirst代码
前言 在前面的文章中我们提到Entity Framework的“Code First”模式也同样可以基于现有数据库进行开发.今天就让我们一起看一下使用Entity Framework Power To ...
- Mysql 该如何 Entity Framework 数据库迁移 和 如何更好的支持EntityFramework.Extended
问题 1.在使用EntityFramework访问Mysql的时候,使用迁移来生成数据库或者更新数据库时候会遇到一些问题 2.EntityFramework.Extended对Mysql的支持不是很完 ...
- Entity Framework数据库迁移
1.启用数据迁移: enable-Migrations2.增加一条数据库迁移指令:add-Migrations 必须带上一个版本名称,比如AddUsernamePassword完整的指令:add-Mi ...
- Entity Framework Core 迁移命令
Add-Migration init Update-Database init 修改model后,执行迁移的命令 更新数据库 每次更新都要{update}修改 Add-Migration {updat ...
- MetadataType来帮助entity framework自动生成的代码进行标注
真的是,用的时候就四处google,还是记在这里容易找 [MetadataType(typeof(Person.Metadata))] public partial class Person { pr ...
- Entity Framework 自动生成代码 如何用继承
分部类 用接口
随机推荐
- Decoding VOX Files in C# (Converting VOX Files to WAV Files)
I wrote a C# class to decode VOX files into WAV files. It follows the Dialogic ADPCM specificationst ...
- html中的table导出Excel
演示地址: http://www.jq22.com/yanshi3312 具体代码: <!DOCTYPE html> <html lang="zh-CN"> ...
- 《Beginning Java 7》 - 2 - Cloning 克隆
Cloning 分两类:影子克隆 shallow cloning 深度克隆 deep cloning * 调用 clone() 需要 implments Cloneable.此函数为 protecte ...
- D. Magic Box(几何)
One day Vasya was going home when he saw a box lying on the road. The box can be represented as a re ...
- adb命令之pm
常用的用法: 查看已经安装的包 pm list packages 查看已经安装的包以及apk路径(-3:只看第三方应用: -s:只看系统应用) -f: see their associated fil ...
- P5030 长脖子鹿放置 最小割
$ \color{#0066ff}{ 题目描述 }$ 如图所示,西洋棋的"长脖子鹿",类似于中国象棋的马,但按照"目"字攻击,且没有中国象棋"别马腿& ...
- P4365 [九省联考2018]秘密袭击coat
$ \color{#0066ff}{ 题目描述 }$ Access Globe 最近正在玩一款战略游戏.在游戏中,他操控的角色是一名C 国士 兵.他的任务就是服从指挥官的指令参加战斗,并在战斗中取胜. ...
- rest-assured之认证授权(Authentication)
rest-assured支持多种认证授权方案,比如:OAuth.digest(摘要认证).certificate(证书认证).form(表单认证)以及preemptive(抢占式基础认证)等.我们可以 ...
- oracle 行列转换函数之WM_CONCAT和LISTAGG的使用(一)
一.wm_concat函数 wm_concat能够实现同样的功能,但是有时在11g中使用需要用to_char()进行转换,否则会出现不兼容现象(WMSYS.WM_CONCAT: 依赖WMSYS 用户, ...
- border.css(解决移动端1px问题)
由于某些机型分辨率过高,会导致1px变成2-多px像素的问题,引用bordercss解决 @charset "utf-8"; .border, .border-top, .bord ...