第一步: 创建一个.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。
Nuget包管理器里面:PM

PM>Scaffold-DbContext "Data Source=.;Initial Catalog=database1;User ID=sa;Password=123456" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Model

指令详细介绍:

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.

覆盖已经有的文件,重新拉取数据库的表生成model

-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 /filepath/Models -Force

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

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

附:

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

第五步:

-OutputDir Models: 生成的文件的存放目录,目前目录是根目录下的Models目录

之后引擎会试图连接你的SQL Server 数据库,并生成文件在你指定的目录里。

在目录中找到一个***Context.cs并打开它,你会发现一个如下方法,

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
optionsBuilder.UseSqlServer(@"{your sql connect string}");
}

如自动生成代码里所写的warning一样,我们不应该把连接字符串放在这里。接下来的工作,让我们来从appsettings.json中读取配置。

在/Models/***Context.cs中添加一个属性用来存放ConnectionString,另外我们需要重写OnConfiguring方法【注释原来方法】,完整的代码应该是这样:

public static string ConnectionString { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(ConnectionString);
}

打开appSetting.json,添加如下代码:

"ConnectionStrings": {
"TestNetCoreEF": "Data Source={your sql server host address};Initial Catalog=TestNetCoreEF;user id={your username};password={your password};"
},

打开 Startup.cs,在ConfigureServices(IServiceCollection services)方法中添加如下代码:

TestNetCoreEFContext.ConnectionString = Configuration.GetConnectionString("TestNetCoreEF");

完整的代码应该是这样:

public void ConfigureServices(IServiceCollection services)
{
//config the db connection string
TestNetCoreEFContext.ConnectionString = Configuration.GetConnectionString("TestNetCoreEF"); // Add framework services.
services.AddMvc();
} 之后的调用和ef一样,


.net core 使用 ef core的更多相关文章

  1. net Core 通过 Ef Core 访问、管理Mysql

    net Core 通过 Ef Core 访问.管理Mysql 本文地址:http://www.cnblogs.com/likeli/p/5910524.html 环境 dotnet Core版本:1. ...

  2. asp.net core + mysql + ef core + linux

    asp.net core + mysql + ef core + linux 以前开发网站是针对windows平台,在iis上部署.由于这次需求的目标服务器是linux系统,就尝试用跨平台的.NET ...

  3. Entity Framework Core(EF Core) 最简单的入门示例

    目录 概述 基于 .NET Core 的 EF Core 入门 创建新项目 更改当前目录 安装 Entity Framework Core 创建模型 创建数据库 使用模型 基于 ASP.NET Cor ...

  4. .net core webapi+EF Core

    .net core webapi+EF Core 一.描述: EF Core必须下载.net core2.0版本 Micorsoft.EntityFrameworkCore:EF框架的核心包Micor ...

  5. 【ASP.NET Core】EF Core - “影子属性” 深入浅出经典面试题:从浏览器中输入URL到页面加载发生了什么 - Part 1

    [ASP.NET Core]EF Core - “影子属性”   有朋友说老周近来博客更新较慢,确实有些慢,因为有些 bug 要研究,另外就是老周把部分内容转到直播上面,所以写博客的内容减少了一点. ...

  6. 一个官翻教程集合:ASP.NET Core 和 EF Core 系列教程

    通过一个大学课程案例讲解了复杂实体的创建过程及讲解 1.ASP.NET Core 和 Entity Framework Core 系列教程——入门 (1 / 10) 2.ASP.NET Core 和 ...

  7. ASP.NET CORE 使用 EF CORE访问数据库

    asp.net core通过ef core来访问数据库,这里用的是代码优先,通过迁移来同步数据库与模型. 环境:vs2017,win10,asp.net core 2.1 一.从建立asp.net c ...

  8. [.NET Core] - 使用 EF Core 的 Scaffold-DbContext 脚手架命令创建 DbContext

    Scaffold-DbContext 命令 参数 Scaffold-DbContext [-Connection] <String> [-Provider] <String> ...

  9. .NET 5/.NET Core使用EF Core 5连接MySQL数据库写入/读取数据示例教程

    本文首发于<.NET 5/.NET Core使用EF Core 5(Entity Framework Core)连接MySQL数据库写入/读取数据示例教程> 前言 在.NET Core/. ...

  10. Asp.net Core 通过 Ef Core 访问、管理Mysql

    本文地址:http://www.cnblogs.com/likeli/p/5910524.html 环境 dotnet Core版本:1.0.0-preview2-003131 本文分为Window环 ...

随机推荐

  1. linux----------centos6.4安装完了以后敲ifconfig,没有局域网ip。解决如下

    1.vim /etc/sysconfig/network-scripts/ifcfg-eth0 进入linux然后进入这个文件里面如下: DEVICE=eth0 HWADDR=00:0C:29:92: ...

  2. js对象属性名驼峰式转下划线

    一.题目示例: 思路: 1.匹配属性名字符串中的大写字母和数字 2.通过匹配后的lastIndex属性获取匹配到的大写字母和数字的位置 3.判断大写字母的位置是否为首位置以及lastIndex是否为0 ...

  3. window中普通用户无法登录远程桌面

    解决方案就是将该用户加到 Remote Desktop Users 这个用户组中. 使用命令 net localgroup "Remote Desktop Users" 用户名 / ...

  4. Axis2的简单配置(完整版)

    Axis2的简单配置(终结版) 1.axis2 下载地址 axis2-1.6.2-bin.zip http://mirror.esocc.com/apache//axis/axis2/java/cor ...

  5. Oarcle 入门之where关键字

    --where 关键字 --作用:过滤行 *将需要的行用where引导出来 用法: 1.判断符号:=,!=,<>,<,>,<=,>=,any,some,all; 例 ...

  6. Oarcle 入门之from关键字

    作用:检索“表” 注意:检索的表后可以添加别名(别名不需要被双引号引起) *每一句都不可缺少

  7. [完美]原生JS获取浏览器版本判断--支持Edge,IE,Chrome,Firefox,Opera,Safari,以及各种使用Chrome和IE混合内核的浏览器

    截至自2017-08-11,支持现世已出的几乎所有PC端浏览器版本判断. 受支持的PC端浏览器列表: Edge IE Chrome Firefox Opera Safari QQ浏览器 360系列浏览 ...

  8. Java继承和组合

    为了保证父类有良好的封装性,不会被子类随意修改,设计父类通常应该遵循以下规则: 1.尽量隐藏父类的内部数据,尽量把父类的所有成员变量设置为 private 访问类型,不要让子类直接访问父类的成员变量: ...

  9. Celery 实现异步任务-one

    celery异步任务: 环境准备 安装celery ,django-celery. 就是一个专注于实时处理和任务调度的分布式队列. 可以异步执行的任务交给后台处理,以防网络阻塞,减小响应时间 cele ...

  10. 剑指offer(16)合并两个排序的链表

    题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 题目分析 重点抓住这两个链表都是单挑递增的,因此我们只需要不断地比较他们的头结点就行,明显这是个 ...