⒈添加依赖

  MySql.Data.EntityFrameworkCore

⒉在appsettings.json配置文件中配置数据库连接字符串

 {
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"ConnectionStrings": {
"MySqlConnection": "server=localhost;port=3306;database=blog;user=root;password=admin"
},
"AllowedHosts": "*"
}

⒊编写数据表实体类及上下文

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; namespace NewDb.Models
{
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; } public ICollection<Post> Posts { get; set; } }
}
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; namespace NewDb.Models
{
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; }
}
}
 using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; namespace NewDb.Models
{
public class BloggingDbContext : DbContext
{
public BloggingDbContext(DbContextOptions<BloggingDbContext> options) : base(options) { } public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
}

⒋使用依赖注入将上下文注册为服务

         public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
}); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); var connection = Configuration["ConnectionStrings:MySqlConnection"]; services.AddDbContext<BloggingDbContext>(options =>
{
options.UseMySQL(connection);
});
}

⒌使用迁移创建数据库

  第一种方式:"Visual Studio 2019" >“工具”>“NuGet 包管理器”>“程序包管理器控制台”,执行以下命令

 Add-Migration InitialCreate -v #搭建迁移基架,以便为模型创建一组初始表
Update-Database -v #创建数据库并向其应用新的迁移

  第二种方式:使用.Net Core CLI,执行以下命令

 dotnet ef migrations add InitialCreate -v #搭建迁移基架,以便为模型创建一组初始表
dotnet ef database update -v #创建数据库并向其应用新的迁移

ASP.NET Core中使用EF Core(MySql)Code First的更多相关文章

  1. 在ASP.NET Core中通过EF Core实现一个简单的全局过滤查询

    前言 不知道大家是否和我有同样的问题: 一般在数据库的设计阶段,会制定一些默认的规则,其中有一条硬性规定就是一定不要对任何表中的数据执行delete硬删除操作,因为每条数据对我们来说都是有用的,并且是 ...

  2. ASP.NET Core 中使用EF Core 将实体映射到数据库表的方法(SQL Server)

    前段时间听过一个关于使用ASP.NET Core建立项目的视频.其中使用EF Core映射到数据库的部分是按部就班地学习.今天自己建立项目时,有些步骤已经有一些遗忘.所以写下这篇文章,顺便理清思路. ...

  3. ASP.NET Core中使用EF Core(MySql)Database First

    ⒈创建数据库,在数据中执行以下脚本. CREATE DATABASE Blogging; USE Blogging; CREATE TABLE Blog ( BlogId int not null P ...

  4. .NET Core中使用EF Core连接MySQL

    最近一直在捣鼓.NET Core方面的东西,顺便写下点东西记录下 1.打开vs2017,新建一个项目 2.vs会自动生成一个项目,然后打开NuGet搜索MySql.Data.EntityFramewo ...

  5. .net core 中使用ef 访问mysql

    1.参考文档说修改项目文件添加,就得这么做,不然会报错 <ItemGroup> <DotNetCliToolReference Include="Microsoft.Ent ...

  6. Asp.net core下利用EF core实现从数据实现多租户(1)

    前言 随着互联网的的高速发展,大多数的公司由于一开始使用的传统的硬件/软件架构,导致在业务不断发展的同时,系统也逐渐地逼近传统结构的极限. 于是,系统也急需进行结构上的升级换代. 在服务端,系统的I/ ...

  7. Asp.net core下利用EF core实现从数据实现多租户(3): 按Schema分离 附加:EF Migration 操作

    前言 前段时间写了EF core实现多租户的文章,实现了根据数据库,数据表进行多租户数据隔离. 今天开始写按照Schema分离的文章. 其实还有一种,是通过在数据表内添加一个字段做多租户的,但是这种模 ...

  8. 万字长文,带你彻底理解EF Core5的运行机制,让你成为团队中的EF Core专家

    在EF Core 5中,有很多方式可以窥察工作流程中发生的事情,并与该信息进行交互.这些功能点包括日志记录,拦截,事件处理程序和一些超酷的最新出现的调试功能.EF团队甚至从Entity Framewo ...

  9. [翻译 EF Core in Action 1.10] 应该在项目中使用EF Core吗?

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

随机推荐

  1. numpy中np.linalg.norm()求向量、矩阵的范数

    np.linalg.norm() # linalg = linear(线性) + algebra(代数),   norm表示范数 x_norm = np.linalg.norm(x, ord=None ...

  2. 支持快应用的http网络库-flyio

    Fly.js 一个基于Promise的.强大的.支持多种JavaScript运行时的http请求库. 有了它,您可以使用一份http请求代码在浏览器.微信小程序.Weex.Node.React Nat ...

  3. cmd中实现代码雨的命令。。。

    颜色修改时不能使用十六进制数 @echo off title digitalrain color 0b setlocal ENABLEDELAYEDEXPANSION for /l %%i in (0 ...

  4. Python3学习笔记(十七):requests模块

    官方中文文档:http://docs.python-requests.org/zh_CN/latest/

  5. [CSP-S模拟测试]:那一天我们许下约定(DP+组合数学)

    题目传送门(内部题2) 输入格式 每个测试点有多组测试数据.对于每组数据,有一行共三个整数$N$,$D$,$M$含义如题.输入结束标识为$“0 0 0”$ (不含引号). 输出格式 对于每组数据,输出 ...

  6. 库&插件&框架&工具

    nodejs 入门 nodejs 入门教程,大家可以在 github 上提交错误 2016 年最好用的表单验证库 SMValidator.js 前端表单验证工具分享 浅谈前端线上部署与运维 说到前端部 ...

  7. InnoDB记录存储结构

    InnoDB是一个将数据存储到磁盘上的存储引擎.对于更新/写入的数据,先将数据从磁盘中加载到内存,更新后再将数据刷新到磁盘. 行格式 而磁盘与内存交互的数据,InnoDB采取将数据分为若干页,一页一般 ...

  8. ubuntu16.04 anaconda3安装

    1.使用清华镜像源下载 wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-5.1.0-Linux-x86_64. ...

  9. nginx中location匹配规则介绍

    一,匹配规则 1,= 表示精确匹配 例子:http://localhost/  将匹配到 location = / {...} http://localhost/aaa  可以匹配到 location ...

  10. LC 963. Minimum Area Rectangle II

    Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these ...