EF Core的安装、EF Core与数据库结合
一.新建一个.net core的MVC项目
新建好项目后,不能像以前一样直接在新建项中添加ef,
需要用命令在添加ef的依赖
二.EF Core实体框架核心安装:
工具> NuGet软件包管理器>软件包管理器控制台
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Tools
Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design
安装成功后就可以在Nuget依赖项中看到
四.更具一个命令就可以从数据库生成model了
方式一:(通过现有数据库创建模型)
Scaffold-DbContext "Server=.;Database=Food;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
该命令会在Models文件夹下生成数据库中的表和上下文对象
注:执行这一步的时候出现了点问题 ,因为系统是win7,powershell版本太低了,不支持这个命令,需要安装
3.0以上的powershell版本才行
添加成功后在models可以看到, 生成了上下文对象与和表对应的model
现在就可以使用EF了
public IActionResult Index()
{ FoodContext fc = new FoodContext(); List<ProType> ptlist = fc.ProType.ToList(); ViewBag.ptlist = ptlist; return View();
}
方式二:(通过模型创建数据库)
1.创建上下文类
public class FoodContext : DbContext
{
public FoodContext (DbContextOptions<FoodContext> options)
: base(options)
{ } public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
} public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; } public List< Post > Posts { get; set; }
} public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; } public int BlogId { get; set; }
public Blog Blog { get; set; }
}
2.在startup.cs的ConfigureServices方法中中将上下文类注册为全局服务:
services.AddDbContext(options => options.UseSqlServer(connection));
3.在appsetting文件中增加连接字符串connection

4.创建数据库
工具 - > NuGet软件包管理器 - >软件包管理器控制台
//创建模型的初始表
Add-Migration InitialCreate
//将新迁移应用于数据库
Update-Database
五.使用依赖注入来装载EF的上下文对象
.net core中用了不少的依赖注入,官方文档中也推荐使用
1:删除方法
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(@"Server=.;Database=Food;Trusted_Connection=True;");
}
2:添加方法
public FoodContext(DbContextOptions<FoodContext> options)
: base(options)
{ }
添加的是一个构造函数用于注入
3:在startup.cs的configureServices方法中添加依赖注入
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc(); services.AddDbContext<FoodContext>(option => {
option.UseSqlServer("Data Source =.; Initial Catalog = EFCore_dbfirst; User ID = sa; Password = sa.123");
}); }
微软官方文档:
https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db
EF Core的安装、EF Core与数据库结合的更多相关文章
- .NET Core项目部署到Linux(Centos7)(五)Centos 7安装.NET Core环境
目录 1.前言 2.环境和软件的准备 3.创建.NET Core API项目 4.VMware Workstation虚拟机及Centos 7安装 5.Centos 7安装.NET Core环境 6. ...
- asp.net core系列 30 EF管理数据库架构--必备知识 迁移
一.管理数据库架构概述 EF Core 提供两种主要方法来保持 EF Core 模型和数据库架构同步.一是以 EF Core 模型为基准,二是以数据库为基准. (1)如果希望以 EF Core 模型为 ...
- EF Core使用笔记(基于MySql数据库)
一.什么是EF Entity Framework 是适用于.NET 的对象关系映射程序 (O/RM). 二.比较 EF Core 和 EF6 1.Entity Framework 6 Entity F ...
- asp.net core 系列 20 EF基于数据模型创建数据库
一.概述 本章使用 Entity Framework Core 构建执行基本数据访问的 ASP.NET Core MVC 应用程序.使用迁移(migrations)基于数据模型创建数据库,是一种cod ...
- EF Core的安装及入门
一.环境准备 1.开发环境:.NET Core 3.1 2.IDE工具:Visual Studio 2019 3.数据库:SQL Server 2012 二.EF Core的安装 1.新建一个项目,如 ...
- .NET CORE 学习笔记之安装EF【Microsoft.EntityFrameworkCore】扩展报错
最近在学习.NET CORE ,刚开始就遇到问题了. 安装EF框架的试试就报错, 报错如下: 错误 程序包还原失败.正在回滚“XXX”的程序包更改. 找了好久的方案,网上也没搜到对应的问题和方案,然而 ...
- [翻译 EF Core in Action 2.0] 查询数据库
Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Cor ...
- asp.net core系列 31 EF管理数据库架构--必备知识 反向工程
一. 反向工程 反向工程是基于数据库架构,生成的实体类和DbContext类代码的过程,对于Visual Studio开发,建议使用PMC.对于其他开发环境,请选择.NET Core CLI工具( ...
- Asp.net core 3.1+EF Core2.2.6+Oracle.EntityFrameworkCore2.1.19连接Oracle数据库
Asp.net Core 3.1+EF Core2.2.6+Oracle.EntityFrameworkCore2.1.19连接Oracle数据库 1.前言 本次主要采用Asp.net core3.1 ...
随机推荐
- ffmpeg 错误 real-time buffer [USB2.0 Camera] [video input] too full or near too full (101% of size: 30412)
利用ffmpeg 获取USB 或者本地摄像机视频,并将视频编码后保存本地文件或者发送到远端流媒体服务经常会出现 类似real-time buffer [USB2.0 Camera] [video in ...
- Android多线程下载大文件解析
1.多线程介绍 用过迅雷的同学都知道.迅雷有个功能叫做多线程.另一个叫离线下载,我们这里重点介绍一下多线程下载.多线程,顾名思义就是非常多歌线程同一时候在执行,为什么要提出多线程这个概念呢?由于有时候 ...
- WPF03(样式)
说起样式,大家第一反应肯定是css,好的,先上一段代码. 1 html{border:0;} 2 ul,form{margin:0; padding:0} 3 body,div,th,td,li,dd ...
- bvlc_reference_caffenet.caffemodel
#uncoding:utf-8 # set up Python environment: numpy for numerical routines, and matplotlib for plotti ...
- stringByAppendingPathComponent和stringByAppendingString 的区别
stringByAppendingPathComponent和stringByAppendingString 的区别 stringByAppendingPathComponent NSString ...
- ACM-BFS之Open the Lock——hdu1195(双向BFS)
这道题的0基础版本号,暴力BFS及题目详情请戳:http://blog.csdn.net/lttree/article/details/24658031 上回书说道,要用双向BFS来尝试一下. 最终A ...
- 三星note3 N900刷机包 4.4.2 ZSUDNE3 官方原汁原味 稳定流畅
ROM介绍 此ROM基于最新的4.4.2 ZSUDNE3 制作,加入一些必要功能,其它性能基本与官方无差距,各方面感觉都非常不错了.此ROM本人自用,所以制作风格有点个人倾向.不论什么建议或者问题欢迎 ...
- iphone开发的技巧
一,改动状态栏: 1.增加[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];但此方法仅仅是不显示状态条,状态 ...
- JAVA虚拟机、Dalvik虚拟机和ART虚拟机简要对照
1.什么是JVM? JVM本质上就是一个软件,是计算机硬件的一层软件抽象,在这之上才干够运行Java程序,JAVA在编译后会生成相似于汇编语言的JVM字节码,与C语言编译后产生的汇编语言不同的是,C编 ...
- EasyDarwin开源云平台接入海康威视EasyCamera摄像机之快照获取与上传
本文转自EasyDarwin团队成员Alex的博客:http://blog.csdn.net/cai6811376 EasyCamera开源摄像机拥有获取摄像机实时快照并上传至EasyDarwin云平 ...