准备工作:

工程:core + console

引用包:

Install-Package Microsoft.EntityFrameworkCore
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Tools
Install-Package Microsoft.Extensions.Configuration.Json

Sqlserver库:127.0.0.1   MyDB  sa  xxx

表:MyTable  --> ID int primaryKey  ……

可以使用Scaffold-DbContext命令生成上下文和实体(此步依赖Microsoft.EntityFrameworkCore.Tools),如下:

Scaffold-DbContext "Data Source=.;Initial Catalog=MyDB;User ID=sa;Password=xxx;MultipleActiveResultSets=true" Microsoft.EntityFrameworkCore.SqlServer -OutputDir models

生成的MyDbContext如下:

public partial class MyDBContext : DbContext
{
public virtual DbSet<MyTable> MyTable { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
//改为读取json配置文件中连接字符串
optionsBuilder.UseSqlServer(Common.configuration.GetSection("connStr").Value);
//optionsBuilder.UseSqlServer(@"Data Source=.;Initial Catalog=MyDB;User ID=sa;Password=@sa123;MultipleActiveResultSets=true");
}
} protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyTable>(entity =>
{
entity.Property(e => e.Id).HasColumnName("ID"); entity.Property(e => e.Address)
.HasMaxLength()
.IsUnicode(false); entity.Property(e => e.Name)
.HasMaxLength()
.IsUnicode(false); entity.Property(e => e.Value)
.HasMaxLength()
.IsUnicode(false);
});
}
}

改为由json配置文件读取连接字符串需引用Microsoft.Extensions.Configuration.Json,当然前提可解析json的包也可以,在console中,json文件属性需设置为“始终复制,内容”,使用如下:

public class Common
{
public static IConfiguration configuration { get; set; }
}
//附加配置文件
Common.configuration = new ConfigurationBuilder().Add(new JsonConfigurationSource() { Path = "project.json" }).Build();
//获取配置文件信息
Common.configuration.GetSection("connStr").Value;
//获取配置文件信息时转为对象
Common.configuration.GetSection("connStr").Get<string>();

测试程序如下:

class Program
{
static void Main(string[] args)
{
Common.configuration = new ConfigurationBuilder().Add(new JsonConfigurationSource() { Path = "project.json" }).Build();
using (MyDBContext context = new MyDBContext())
{
var model = context.MyTable.Find();
var list = context.MyTable.FromSql(new RawSqlString("select * from mytable")).ToListAsync().Result;
}
Console.ReadKey();
}
}

core EFCore 开始尝试的更多相关文章

  1. .Net core,EFCore 入门

    我在百度上搜了一下.net  core和efcore 入门案例.好多博客都是大概说了一下做法,对于小白而言还是一头雾水,我今天就抽出一点时间,写一个详细的入门小案例,就一张表没有什么业务可言.主要是操 ...

  2. ASP.NET Core EFCore 之Code First

    1.在.NET Core项目中使用Nuget引用包 Sql Server 请安装 Microsoft.EntityFrameworkCore.SqlServer 2.添加实体类 [Table(&quo ...

  3. 旧 WCF 项目迁移到 asp.net core + gRPC 的尝试

    一个月前,公司的运行WCF的windows服务器down掉了,由于 AWS 没有通知,没有能第一时间发现问题. 所以,客户提出将WCF服务由C#改为JAVA,在Linux上面运行:一方面,AWS对Li ...

  4. .net core EFcore model生成数据

    创建数据库 (扫盲贴还劳烦大神们勿喷,谢谢) 打开数据库 输入如下代码 创建数据库 CREATE DATABASE [Blogging]; GO USE [Blogging]; GO CREATE T ...

  5. 使用.net core efcore根据数据库结构自动生成实体类

    源码 github,已更新最新代码 https://github.com/leoparddne/GenEntities/ 使用的DB是mysql,所有先nuget一下mysql.data 创建t4模板 ...

  6. .net core EFCore CodeFirst 迁移出现错误【No project was found. Change the current working directory or use the --project option. 】

    PM> dotnet ef Migrations add Init No project was found. Change the current working directory or u ...

  7. ASP.NET Core EFCore 之DBFirst 自动创建实体类和数据库上下文

    通过引用Nuget包添加实体类 运行 Install-Package Microsoft.EntityFrameworkCore.SqlServer 运行 Install-Package Micros ...

  8. ASP.NET CORE系列【三】使用Entity Framework Core进行增删改查

    身份验证 以前我们熟悉的web.config中配置的form验证,现在没有了.我们来看看在Core里面如何配置: 首先需要NuGet安装一个包:Microsoft.AspNetCore.Authent ...

  9. ASP.NET Core 中文文档 第四章 MVC(4.4)依赖注入和控制器

    原文: Dependency Injection and Controllers 作者: Steve Smith 翻译: 刘浩杨 校对: 孟帅洋(书缘) ASP.NET Core MVC 控制器应通过 ...

随机推荐

  1. SQL Server 2008 存储过程示例

    出处:http://www.jb51.net/article/54730.htm --有输入参数的存储过程-- create proc GetComment (@commentid int) as s ...

  2. LeetCode题解:(221) Maximal Square

    题目说明 Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's a ...

  3. MFC各种属性设置

    在使用MFC的时候经常需要对例如对话框的外观进行一些设置.MFC哪些属性的含义和设置可以参照博客: http://www.cnblogs.com/lzmfywz/archive/2012/04/20/ ...

  4. [C/C++] 输入函数getline(cin,str) 与cin.getline(str,int)区别

    cin.getline()函数是处理数组字符串的,其原型为cin.getline(char * , int),第一个参数为一个char指针,第二个参数为数组字符串长度. getline(cin,str ...

  5. POJ1815_Friendship

    一个无向图,问你删除多少点后,可以隔断起点到终点的所有路径?输出字典序最小的删点方案. 求最小点割,先拆点,容量为1,普通边容量无穷,最大流即为应删点数. 需要求出字典序最小的方案,可以从小到大枚举所 ...

  6. python自动化之图像

    ''' RGBA值:指定颜色中的红.绿.蓝和alpha(透明度)的值 RGBA                                            名称 (255,255,255,2 ...

  7. nowcoder 203J Graph Coloring I(dfs)

    题目链接 题目描述 修修在黑板上画了一些无向连通图,他发现他可以将这些图的结点用两种颜色染色,满足相邻点不同色. 澜澜不服气,在黑板上画了一个三个点的完全图.修修跟澜澜说,这个图我能找到一个简单奇环. ...

  8. iOS 企业账号申请证书和打包ipa

    准备: 299美元的企业账号. 1.登陆苹果开发者中心: https://developer.apple.com .点击Menber Center.输入企业账号和密码登陆. 2.登陆后选择“Certi ...

  9. 打豪车应用:uber详细攻略(附100元优惠码)

    在嘀嘀打车和快的打车交战热闹的时候,美国的打车应用uber进入中国.与在美国以个人司机注册做 Uber 司机为主的模式不同,Uber 在中国采用与租车公司合作.由租车公司提供车辆和司机的模式,同时中文 ...

  10. jingchi.ai 2017.11.25-26 Onsite面试

    时间:2017.11.25 - 11.26 地点:安徽安庆 来回路费报销,住宿报销. day1: 大哥哥问了我一个实际中他们遇到的问题.有n个点,将点进行分块输出,输出各个块的均值点.具体就是100* ...