1、新建ASP.NET Core Web应用程序

2、从NuGet下载安装以下工具包

Microsoft.EntityFrameworkCore

Microsoft.EntityFrameworkCore.SqlServer

Microsoft.EntityFrameworkCore.Tools

Microsoft.EntityFrameworkCore.Design

Microsoft.EntityFrameworkCore.SqlServer.Design

3、然后,我们在VS的工具选项中,选择NuGet包管理器,选择程序包管理控制台(其中Models2是输出文件夹)

Scaffold-DbContext "Server=.;database=test1;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

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

5、注释上下文对象里的固化连接字符串

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

  "ConnectionStrings": { "SqlServer": "Server=.;database=test;Trusted_Connection=True;" },

7、然后我们在Startup中注入我们的上下文对象和获取数据库连接串

//注入上下文对象
services.AddDbContext<testContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("SqlServer")));

8、创建控制器,代码如下

public class FirstController : Controller
{
//构造函数注入上下文
private readonly testContext _context;
public FirstController(testContext Context)
{
_context = Context;
}

// GET: /<controller>/
public IActionResult Index(int? id)
{
base.ViewData["User1"] = new CurrentUser()
{
Id = _context.TestAutoid.Find(1).Autoid1,
Name = _context.TestAutoid.Find(1).Autoid2.ToString(),
Account = _context.TestAutoid.Find(1).Autoid3.ToString(),
Email = _context.TestAutoid.Find(1).Autoid4.ToString(),
Password = _context.TestAutoid.Find(1).Autoid5.ToString(),

};

base.ViewData["Something"] = 12345;

base.ViewBag.Name = "Eleven";
base.ViewBag.Description = "Teacher";
base.ViewBag.User = new CurrentUser()
{
Id = 7,
Name = "IOC",
Account = "限量版",
Email = "莲花未开时",
Password = "落单的候鸟",
LoginTime = DateTime.Now
};

base.TempData["User"] = new CurrentUser()
{
Id = 7,
Name = "CSS",
Account = "季雨林",
Email = "KOKE",
Password = "落单的候鸟",
LoginTime = DateTime.Now
};//后台可以跨action 基于session

if (id == null)
{
//return this.Redirect("~/First/TempDataPage");//未完待续
//return this.re
return this.Redirect("~/First/TempDataPage");
}

else
return View(new CurrentUser()
{
Id = 7,
Name = "一点半",
Account = "季雨林",
Email = "KOKE",
Password = "落单的候鸟",
LoginTime = DateTime.Now
});
}

}

9、创建相应的视图如下:

@model NetCore2._2.MVC6.Models.CurrentUser
@using NetCore2._2.MVC6.Models;

@{
ViewBag.Title = "Index";
CurrentUser userViewData = ViewData["User1"] as CurrentUser;//ViewData需要类型转换
CurrentUser userViewBag = ViewBag.User;//ViewBag直接用
CurrentUser userOther = ViewBag.User1;
}

<div class="row">
<div class="col-md-4">
<h2>Getting started</h2>
@base.Model.Name
<p>@(((CurrentUser)ViewData["User1"]).Name)</p>
<p>@userViewData.Name</p>
<p>@userViewBag.Account</p>
<p>@userOther.Name</p>
<p>@(((CurrentUser)ViewBag.User).Name)</p>
<p>@(((CurrentUser)TempData["User"]).Name)</p>
<p>@base.Model.Name</p>
</div>

</div>

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

.NET Core整理之配置EFCore的更多相关文章

  1. .net core web api + Autofac + EFCore 个人实践

    1.背景 去年时候,写过一篇<Vue2.0 + Element-UI + WebAPI实践:简易个人记账系统>,采用Asp.net Web API + Element-UI.当时主要是为了 ...

  2. 扒一扒.NET Core的环境配置提供程序

    很久之前,在玩Docker的时候顺便扒了扒,最近,终于下定决心花了些时间整理并成文,希望能够给大家一些帮助. 目录 .NET Core中的配置 ASP.NET Core中的配置 扒一扒环境变量提供程序 ...

  3. 运维开发笔记整理-URL配置

    运维开发笔记整理-URL配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.URL路由 对于高质量的Web应用来说,使用简洁,优雅的URL的路由是一个非常值得重视的细节.Dja ...

  4. .NET Core技术研究-配置读取

    升级ASP.NET Core后,配置的读取是第一个要明确的技术.原先的App.Config.Web.Config.自定义Config在ASP.NET Core中如何正常使用.有必要好好总结整理一下,相 ...

  5. MVC + EFCore 项目实战 - 数仓管理系统2- 搭建基本框架配置EFCore

    本次课程就正式进入开发部分. 首先我们先搭建项目框架,还是和之前渐进式风格保持一致,除必备组件外,尽量使用原生功能以方便大家理解. 开发工具:vs 2019 或以上 数据库:SQL SERVER 20 ...

  6. ASP.NET Core开发-如何配置Kestrel 网址Urls

    ASP.NET Core中如何配置Kestrel Urls呢,大家可能都知道使用UseUrls() 方法来配置. 今天给介绍全面的ASP.NET Core 配置 Urls,使用多种方式配置Urls. ...

  7. asp.net core 将配置文件配置迁移到数据库(一)

    asp.net core 将配置文件配置迁移到数据库(一) Intro asp.net core 配置默认是项目根目录下的 appsettings.json 文件,还有环境变量以及 command l ...

  8. asp.net core 系列 10 配置configuration (上)

    一.  ASP.NET Core 中的配置概述 ASP.NET Core 中的应用配置是基于键值对,由configuration 程序提供. configuration  将从各种配置源提供程序操作键 ...

  9. .Net core下的配置设置(二)——Option

    我在前面的文章.Net core下的配置设置(一)——Configuration中介绍了.net core下配置文件的读取方法,在.net core中,直接从Configuration对象中读取的并不 ...

随机推荐

  1. SQL Server 删除重复记录,只保留一条记录

    原文地址:http://blog.csdn.net/eriato/article/details/17417303 有张表格之前没有设计关键字段的唯一约束,导致有时候执行插入操作时不小心执行了多次就出 ...

  2. 18 ArcGIS API for JavaScript4.X 系列加载天地图(经纬度)

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

  3. 使用SSM重新开发计科院网站

    一.游览 在游览器地址栏输入:http://localhost:8080/index,即访问计科院首页,由于前期对数据库以及JavaBean的设计考虑不够充分,导致后期的代码臃肿,所以项目启动时对首页 ...

  4. 如何在微信小程序定义全局变量、全局函数、如何实现 函数复用 模块化开发等问题详解

    1.如何定义全局数据 在app.js的App({})中定义的数据或函数都是全局的,在页面中可以通过var app = getApp();  app.function/key的方式调用,不过我们没有必要 ...

  5. Java软件工程师面试题:Java运行时异常与一般异常有什么不一样?

    异常表示程序运行过程中可能出现的非正常状态,运行时异常表示虚拟机的通常操作中可能遇到的异常,是一种常见运行错误.java编译器要求方法必须声明抛出可能发生的非运行时异常,但是并不要求必须声明抛出未被捕 ...

  6. PHP 点阵5*7字体

    效果: 源码: <?php // standard ascii 5x7 font 纵向取模 // defines ascii characters 0x20-0x7f (32-127) $fon ...

  7. [Swift]LeetCode122. 买卖股票的最佳时机 II | Best Time to Buy and Sell Stock II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  8. [Swift]LeetCode622. 设计循环队列 | Design Circular Queue

    Design your implementation of the circular queue. The circular queue is a linear data structure in w ...

  9. [Swift]LeetCode891. 子序列宽度之和 | Sum of Subsequence Widths

    Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the  ...

  10. 【异常】Servlet.service() for servlet [springMvc] in context with path [/orderdishessystem] threw exception [Handler processing failed; nested exception is java.lang.NoClassDefFoundError: net/sf/ezmorph/M

    今天做登录的时候,引入json-lib-2.1-jdk15.jar的包时,执行到JSONObject jsonObject = new JSONObject()对象就报标题的那个错. 原来是除了要导入 ...