今天有一个小任务:要把一个数据的数据搬运到另一个数据库(两个数据库的数据结构很不一样)。

决定用.net core  console app来跑,并且采用entityframework 去做数据CRUD.

环境:win10 + vs2017rc

第一步: 创建一个.net core console app。

第二步:安装EFCore package 和  design(以前vs是有EF项目模板的,core版本现在没有,所有安装这个工具来创建ModelsType Context等).

工具-->Nuget包管理器-->程序包管理控制台

1.Install-package microsoft.entityframeworkcore.sqlserver

2.Install-package microsoft.entityframeworkcore.sqlserver.design

3.Install-Package Microsoft.EntityFrameworkCore.Tools –Pre    (-Pre应该是预览版的意思)

第三步:使用工具的scaffold-dbcontext(数据库上下文脚手架)指令来生成models和context。

指令详细介绍:

Scaffold-DbContext [-Connection] <String> [-Provider] <String> [-OutputDir <String>] [-Context <String>]
[-Schemas <String>] [-Tables <String>] [-DataAnnotations] [-Force] [-Project <String>]
[-StartupProject <String>] [-Environment <String>] [<CommonParameters>]

PARAMETERS
-Connection <String>
Specifies the connection string of the database.

-Provider <String>
Specifies the provider to use. For example, Microsoft.EntityFrameworkCore.SqlServer.

-OutputDir <String>
Specifies the directory to use to output the classes. If omitted, the top-level project directory is used.

-Context <String>
Specifies the name of the generated DbContext class.

-Schemas <String>
Specifies the schemas for which to generate classes.

-Tables <String>
Specifies the tables for which to generate classes.

-DataAnnotations [<SwitchParameter>]
Use DataAnnotation attributes to configure the model where possible. If omitted, the output code will use only the fluent API.

-Force [<SwitchParameter>]
Force scaffolding to overwrite existing files. Otherwise, the code will only proceed if no output files would be overwritten.

-Project <String>
Specifies the project to use. If omitted, the default project is used.

-StartupProject <String>
Specifies the startup project to use. If omitted, the solution's startup project is used.

-Environment <String>
Specifies the environment to use. If omitted, "Development" is used.

本文实例:

Scaffold-dbcontext "Server=192.168.1.159;database=SGD.Invest;Integrated Security=false;user id=****;password=*****" Microsoft.EntityFrameworkCore.SqlServer -outputdir v1/models

Integrated Security(是否集成认证 windows账户认证的意思)

第四步:上面我们已经看到Context文件已经生成,只需要生成它的实例就可以使用了。

附:

配置dbcontext的官方文档:https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext

从EntityFramework转换EntityFrameworkCore的正确姿势(DBFirst)的更多相关文章

  1. int转换char的正确姿势

    一:背景 在一个项目中,我需要修改一个全部由数字(0~9)组成的字符串的特定位置的特定数字,我采用的方式是先将字符串转换成字符数组,然后利用数组的位置来修改对应位置的值.代码开发完成之后,发现有乱码出 ...

  2. 程序员取悦女朋友的正确姿势---Tips(iOS美容篇)

    前言 女孩子都喜欢用美图工具进行图片美容,近来无事时,特意为某人写了个自定义图片滤镜生成器,安装到手机即可完成自定义滤镜渲染照片.app独一无二,虽简亦繁. JH定律:魔镜:最漂亮的女人是你老婆魔镜: ...

  3. Golang错误和异常处理的正确姿势

    Golang错误和异常处理的正确姿势 错误和异常是两个不同的概念,非常容易混淆.很多程序员习惯将一切非正常情况都看做错误,而不区分错误和异常,即使程序中可能有异常抛出,也将异常及时捕获并转换成错误.从 ...

  4. 程序员取悦女票的正确姿势---Tip1(iOS美容篇)

    代码地址如下:http://www.demodashi.com/demo/11695.html 前言 女孩子都喜欢用美图工具进行图片美容,近来无事时,特意为某人写了个自定义图片滤镜生成器,安装到手机即 ...

  5. 读取ClassPath下resource文件的正确姿势

    1.前言 为什么要写这篇文章?身为Java程序员你有没有过每次需要读取 ClassPath 下的资源文件的时候,都要去百度一下,然后看到下面的这种答案: Thread.currentThread(). ...

  6. 剖析和解决Python中网络粘包的正确姿势

    目录 1.粘包及其成因 1.1.粘包产生 1.2.粘包产生的原因 2.尝试解决粘包 2.1.指定数据包的长度 2.2.固定数据包的长度 2.3.用函数实现多次调用发送数据 3.解决粘包问题的正确姿势 ...

  7. Flutter Webview添加Cookie的正确姿势

    场景 h5页面要从cookie里面取数据,所以需要在flutter webview的cookie里面塞一些数据,设置的数据多达十几条:按照网上查的使用方式来设置,通过fiddler抓包发现,只能生效一 ...

  8. 判断是否为gif/png图片的正确姿势

    判断是否为gif/png图片的正确姿势 1.在能取到图片后缀的前提下 1 2 3 4 5 6 7 8 9 //假设这是一个网络获取的URL NSString *path = @"http:/ ...

  9. 在Linux(ubuntu server)上面安装NodeJS的正确姿势

    上一篇文章,我介绍了 在Windows中安装NodeJS的正确姿势,这一篇,我们继续来看一下在Linux上面安装和配置NodeJS. 为了保持一致,这里也列举三个方法 第一个方法:通过官网下载安装 h ...

随机推荐

  1. H5动效的常见制作手法

    众所周知,一个元素,动往往比静更吸引眼球: 一套操作界面,合适的动态交互反馈能给用户带来更好的操作体验: 一个H5运营宣传页,炫酷的动画特效定能助力传播和品牌打造. 近两年,小到loading动画,表 ...

  2. 常用CSS代码片段常见css bug

    1.禁止文字被选中 .unselectable { -moz-user-select: -moz-none; -khtml-user-select: none; -webkit-user-select ...

  3. hibernate简单集合映射和获取

    简单集合映射(可以直接获取) // javabean设计 public class User { private int userId; private String userName; // 一个用 ...

  4. 前端开发—Javascript

    Javascript 语言简介: 语言规范: 注释:/  这是单行注释 /   /* 换行*/ 多行注释 结束符: :分号 语法基础 变量 变量声明 1 变量名可以是 数字 字母 下划线 $ 组成,不 ...

  5. Spring Boot project with static content generates 404 when running jar

    转自:http://stackoverflow.com/questions/21358403/spring-boot-project-with-static-content-generates-404 ...

  6. web service服务是查询QQ用户是否在线

    使用php5开发客户端: <?php try { //$client = new SoapClient("HelloService.wsdl",array('encoding ...

  7. EasyUI闪屏,EasyUI页面加载提示:原理+代码+效果图

    使用EasyUI时,有个经常遇到的问题,页面还没有渲染完成的时候,就展现了. 刚刚开始很混乱,等加载完成后,就好了. 参考这篇文章http://blog.csdn.net/zheng0518/arti ...

  8. POJ 3228 Gold Transportation

    Gold Transportation Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Ori ...

  9. BA-siemens-insight在win7下如何配置opc接口

    一.运行环境:win7(OPC接口在win_xp下配置需安装插件,不好意思没搞定,现在只有win7系统32位下的教程了) 由于OPC(OLE for Process Control)建立在Micros ...

  10. Android开发之使用BroadcastReceiver实现开机自己主动启动(源码分享)

    上一节已经介绍过BroadcastReceiver实现实时监听电量的功能,这节就来介绍一下假设实现开机自己主动启动的功能.这个比监听电量还简单不少 (1)在清单文件注冊权限 <uses-perm ...