.NET Core整理之配置EFCore
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的更多相关文章
- .net core web api + Autofac + EFCore 个人实践
1.背景 去年时候,写过一篇<Vue2.0 + Element-UI + WebAPI实践:简易个人记账系统>,采用Asp.net Web API + Element-UI.当时主要是为了 ...
- 扒一扒.NET Core的环境配置提供程序
很久之前,在玩Docker的时候顺便扒了扒,最近,终于下定决心花了些时间整理并成文,希望能够给大家一些帮助. 目录 .NET Core中的配置 ASP.NET Core中的配置 扒一扒环境变量提供程序 ...
- 运维开发笔记整理-URL配置
运维开发笔记整理-URL配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.URL路由 对于高质量的Web应用来说,使用简洁,优雅的URL的路由是一个非常值得重视的细节.Dja ...
- .NET Core技术研究-配置读取
升级ASP.NET Core后,配置的读取是第一个要明确的技术.原先的App.Config.Web.Config.自定义Config在ASP.NET Core中如何正常使用.有必要好好总结整理一下,相 ...
- MVC + EFCore 项目实战 - 数仓管理系统2- 搭建基本框架配置EFCore
本次课程就正式进入开发部分. 首先我们先搭建项目框架,还是和之前渐进式风格保持一致,除必备组件外,尽量使用原生功能以方便大家理解. 开发工具:vs 2019 或以上 数据库:SQL SERVER 20 ...
- ASP.NET Core开发-如何配置Kestrel 网址Urls
ASP.NET Core中如何配置Kestrel Urls呢,大家可能都知道使用UseUrls() 方法来配置. 今天给介绍全面的ASP.NET Core 配置 Urls,使用多种方式配置Urls. ...
- asp.net core 将配置文件配置迁移到数据库(一)
asp.net core 将配置文件配置迁移到数据库(一) Intro asp.net core 配置默认是项目根目录下的 appsettings.json 文件,还有环境变量以及 command l ...
- asp.net core 系列 10 配置configuration (上)
一. ASP.NET Core 中的配置概述 ASP.NET Core 中的应用配置是基于键值对,由configuration 程序提供. configuration 将从各种配置源提供程序操作键 ...
- .Net core下的配置设置(二)——Option
我在前面的文章.Net core下的配置设置(一)——Configuration中介绍了.net core下配置文件的读取方法,在.net core中,直接从Configuration对象中读取的并不 ...
随机推荐
- jieba库与好玩的词云的学习与应用实现
经过了一些学习与一些十分有意义的锻(zhe)炼(mo),我决定尝试一手新接触的python第三方库 ——jieba库! 这是一个极其优秀且强大的第三方库,可以对一个文本文件的所有内容进行识别,分词,甚 ...
- 《SQL优化入门》讲座总结
MySQL运行机制 MySQL每个query只能运行在一个CPU上,更多的CPU,更快的CPU会更有利于并发 MySQL执行计划 Using filesort: 表示无法利用索引完成排序,也有可能是因 ...
- 找一个数组的最大和的连续子数组(时间复杂度 O(n))
设计思想 一开始的思想是求出全部的情况,再分别比较大小,这种方法适用于有限个数组,不适用于输入数组长度和内容的情况. 但也试着做了 int a[]= {-1,2,6,-10}; int size=4; ...
- Web版需求征集系统所得2,servlet中request.getParameter获值乱码问题解决
servlet获值乱码问题解决 解决办法一(最简单有效) request.setCharacterEncoding("utf-8"); 解决办法二 因为乱码问题的产生是因为默认格式 ...
- 错误:Java HotSpot(TM) 64-Bit Server VM warning: Insufficient space for shared memory file
Java HotSpot(TM) 64-Bit Server VM warning: Insufficient space for shared memory file: /tmp/hsperfdat ...
- python从入门到实践-5章if语句
#!/user/bin/env python cars = ['audi','bmw','subaru','toyota']for car in cars: if car == 'bmw': prin ...
- JS数组slice()和splice()的区别
以前还是纯小白的时候,总会搞混JS数组的 slice() 和 splice() 方法.因为这2个方法名字太像了,就差一个字母,语法也有类似之处. 现在久了没用,有时候也会忘记,所以做一个总结来区 ...
- [Swift]LeetCode79. 单词搜索 | Word Search
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- [Swift]LeetCode398. 随机数索引 | Random Pick Index
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- [Swift]LeetCode747. 至少是其他数字两倍的最大数 | Largest Number At Least Twice of Others
In a given integer array nums, there is always exactly one largest element. Find whether the largest ...