前言

很久没写博客了,因为真的很忙,终于空下来,打算学习一下EF Core顺便写个系列, 今天我们就来看看第一篇DBFirst.

本文环境:VS2017  Win7  .NET Core1.1    EF Core1.1.2

正文

这里我们不讨论是用DBFirst好,还是CodeFirst高端..各有各自的用处和适用场景..

我们单纯的只是来使用这个DBFirst..

既然是DBFirst,那么在用DBFirst之前..首先你要有一个数据库(嗯,废话)

其次,如果你是Windows7系统 那么需要升级你的Windows PowerShell到3.0+的版本

然后你需要安装相关的工具包,从NuGet下载即可如下图:

为了方便你们复制..我列一下:

Microsoft.EntityFrameworkCore

Microsoft.EntityFrameworkCore.SqlServer

Microsoft.EntityFrameworkCore.Tools

Microsoft.EntityFrameworkCore.Design

Microsoft.EntityFrameworkCore.SqlServer.Design

然后,我们在VS的工具选项中,选择NuGet包管理器,选择程序包管理控制台

输入命令行:

Scaffold-DbContext "这里输入你的数据库连接字符串" Microsoft.EntityFrameworkCore.SqlServer

就会生成一个Modles文件夹如图:

这里面就是你的上下文对象和相关的实体类了.

我们进到上下文对象,会发现自己的连接字符串是固化在这里面的,如图:

我们当然不能这么做,所以,请删除掉他.

下一步,我们使用Asp.net Core 来测试测试看能不能访问.

创建Core项目的流程,我就不说了

然后给你的Core项目用NuGet添加引用:Microsoft.EntityFrameworkCore.SqlServer和Microsoft.EntityFrameworkCore

在配置文件里添加数据库连接字符串:

  "ConnectionStrings": {
"SchoolConnection": "Data Source=.;Initial Catalog=School_Test;User ID=**;Password=***;MultipleActiveResultSets=true"
}

然后我们在Startup中注入我们的上下文对象:

在ConfigureServices()方法中注入,代码如下:

        public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
//注入SignalR.(与本文无关,请无视)
services.AddSignalR(options =>
{
options.Hubs.EnableDetailedErrors = true;
});
//注入上下文对象
services.AddDbContext<School_TestContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("SchoolConnection")));
services.AddMvc();
}

我们创建控制器,代码如下:

    public class TestController : Controller
{
//构造函数注入上下文
private readonly School_TestContext _context;
public TestController(School_TestContext Context)
{
_context = Context;
} public IActionResult ListView()
{
return View(_context.UserTable.ToList());
}
}

创建相应的视图如下:

@model IEnumerable<EFCoreModel.Modles.UserTable>
@{
ViewData["Title"] = "ListView";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>ListView</h2> <p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
用户名
</th>
<th>
密码
</th>
<th>
ID
</th>
<th>
班级名
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.PassWord)
</td>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Class.ClassName)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>

运行代码,会报错.如下错误:

No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.

这是因为我们通过DBFirst生成的上下文并不能直接拿来注入使用.我们需要改造一下,给上下文添加构造函数,如下:

        public School_TestContext(DbContextOptions options) :base(options)
{ }

然后在运行我们的代码.得到结果如下:

我们发现红框位置的作为关联表的班级名,并没有显示~,这个留待我们后面讲解.

一步步学习EF Core(1.DBFirst)的更多相关文章

  1. 一步步学习EF Core(3.EF Core2.0路线图)

    前言 这几天一直在研究EF Core的官方文档,暂时没有发现什么比较新的和EF6.x差距比较大的东西. 不过我倒是发现了EF Core的路线图更新了,下面我们就来看看 今天我们来看看最新的EF Cor ...

  2. 一步步学习EF Core(2.事务与日志)

    前言 上节我们留了一个问题,为什么EF Core中,我们加载班级,数据并不会出来 其实答案很简单,~ 因为在EF Core1.1.2 中我们在EF6.0+中用到的的延迟加载功能并没有被加入,不过在EF ...

  3. [转]一步步学习EF Core(2.事务与日志)

    本文转自:http://www.cnblogs.com/GuZhenYin/p/6862505.html 上节我们留了一个问题,为什么EF Core中,我们加载班级,数据并不会出来 其实答案很简单,~ ...

  4. EF Core学习Code First

    下面通过实例来学习EF Core Code First,也就是通过EF Core迁移来完成从模型生成数据库. 本实例使用EntityFrameworkCore SQLite 数据库进行介绍,大家也可以 ...

  5. [翻译 EF Core in Action 2.3] 理解EF Core数据库查询

    Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Cor ...

  6. [翻译 EF Core in Action 2.2] 创建应用程序的数据库上下文

    Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Cor ...

  7. EF Core in Action 中文翻译 第一部分导航

    Entityframework Core in action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Core ...

  8. [翻译 EF Core in Action 2.1] 设置一个图书销售网站的场景

    Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Cor ...

  9. [翻译 EF Core in Action 2.0] 查询数据库

    Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Cor ...

随机推荐

  1. javascript的闭包与一次重构的感受

    有没有这么一个场景,你的一个动作需要在所有异步方法执行完毕后,再进行操作?然而你对异步方法何时执行完毕感到困扰,只能在每个方法中写回调,在回调中重复劳动? 偶然的,想起了之前经理讲过的闭包的概念,偶然 ...

  2. C++ 拷贝控制和资源管理,智能指针的简单实现

    C++ 关于拷贝控制和资源管理部分的笔记,并且介绍了部分C++ 智能指针的概念,然后实现了一个基于引用计数的智能指针.关于C++智能指针部分,后面会有专门的研究. 通常,管理类外资源的类必须定义拷贝控 ...

  3. 老李分享:大数据测试中java和hadoop关系

    Hadoop的创始人是Doug Cutting, 同时也是著名的基于Java的检索引擎库Apache Lucene的创始人.Hadoop本来是用于著名的开源搜索引擎Apache Nutch,而Nutc ...

  4. css3 loading动画 三个省略号

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  5. es6 module + webpack

    其实在之前本人就看了 es6 里面的一部分内容,当然是阮一峰大神的 ECMAScript 6 入门. 最近闲来无事又来看下,其中 Module 的语法 这章时候,用里面代码跑的时候,理所当然的报错 S ...

  6. 1020. Tree Traversals

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  7. Historical节点

    Historical节点 Historical 节点的作用是,load 历史数据提供查询. 运行类 io.druid.cli.Main server historical 装载和保存Segments ...

  8. this web application instance has been stopped already.

    this web application instance has been stopped already. Could not load oracle/sql/converter_xcharset ...

  9. 短路运算|字符串操作函数|内存mem操作函数

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  10. 安卓Native和H5页面进行交互

    安卓Native和H5页面进行交互 1.H5页面调用安卓Native界面 1)通过给webView添加JsInterface,安卓提供接口,让H5来进行调用    a)安卓写一个类,里面的方法需要用通 ...