⒈添加依赖

  MySql.Data.EntityFrameworkCore

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

  1. {
  2. "Logging": {
  3. "LogLevel": {
  4. "Default": "Warning"
  5. }
  6. },
  7. "ConnectionStrings": {
  8. "MySqlConnection": "server=localhost;port=3306;database=blog;user=root;password=admin"
  9. },
  10. "AllowedHosts": "*"
  11. }

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5.  
  6. namespace NewDb.Models
  7. {
  8. public class Blog
  9. {
  10. public int BlogId { get; set; }
  11. public string Url { get; set; }
  12.  
  13. public ICollection<Post> Posts { get; set; }
  14.  
  15. }
  16. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5.  
  6. namespace NewDb.Models
  7. {
  8. public class Post
  9. {
  10. public int PostId { get; set; }
  11. public string Title { get; set; }
  12. public string Content { get; set; }
  13.  
  14. public int BlogId { get; set; }
  15. public Blog Blog { get; set; }
  16. }
  17. }
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6.  
  7. namespace NewDb.Models
  8. {
  9. public class BloggingDbContext : DbContext
  10. {
  11. public BloggingDbContext(DbContextOptions<BloggingDbContext> options) : base(options) { }
  12.  
  13. public DbSet<Blog> Blogs { get; set; }
  14. public DbSet<Post> Posts { get; set; }
  15. }
  16. }

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

  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. services.Configure<CookiePolicyOptions>(options =>
  4. {
  5. // This lambda determines whether user consent for non-essential cookies is needed for a given request.
  6. options.CheckConsentNeeded = context => true;
  7. options.MinimumSameSitePolicy = SameSiteMode.None;
  8. });
  9.  
  10. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  11.  
  12. var connection = Configuration["ConnectionStrings:MySqlConnection"];
  13.  
  14. services.AddDbContext<BloggingDbContext>(options =>
  15. {
  16. options.UseMySQL(connection);
  17. });
  18. }

⒌使用迁移创建数据库

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

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

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

  1. dotnet ef migrations add InitialCreate -v #搭建迁移基架,以便为模型创建一组初始表
  2. 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. CUDA-F-2-2-核函数计时

    Abstract: 本文介绍CUDA核函数计时方法 Keywords: gettimeofday,nvprof 开篇废话 继续更新CUDA,同时概率和数学分析也在更新,欢迎大家访问www.face2a ...

  2. cygwin执行.py提示找不到模块,但已经安装模块的解决办法

    . 在解决了cygwin中make命令不能使用的问题之后(https://www.cnblogs.com/zhenggege/p/10724122.html),make maskrcnn路径下的set ...

  3. Note 2 for <Pratical Programming : An Introduction to Computer Science Using Python 3>

    Book Imformation : <Pratical Programming : An Introduction to Computer Science Using Python 3> ...

  4. list元素按照日期排序

    private static void ListSort(List<AppClassInfoVo> list) { Collections.sort(list, new Comparato ...

  5. Android RecyclerView与ListView比较

    RecyclerView 概述 RecyclerView 集成自 ViewGroup .RecyclerView是Android-support-V7版本中新增的一个Widgets,官方对于它的介绍是 ...

  6. Movidius的深度学习入门

    1.Ubuntu虚拟机上安装NC SDK cd /home/shine/Downloads/ mkdir NC_SDK git clone https://github.com/movidius/nc ...

  7. [SQL Server常用系统存储过程大全]

    1.  sp_help   报告有关数据库对象(sys.sysobjects 兼容视图中列出的所有对象)  sp_help    表名称,存储过程名称等 2.  sp_helpdb   报告有关数据库 ...

  8. win10搜索框突然不能使用了

    备忘: win10搜索不出来了,使用以下方法恢复了,备忘下 1,首先打开任务管理器 重新启动wservice服务 2.发现这时候搜索依然不能使用 然后重新启动explorer.exe (1)右键关闭该 ...

  9. GitHub-Tech-DotNet:Cnblogs

    ylbtech-GitHub-Tech-DotNet:Cnblogs 1.返回顶部 · EnyimMemcachedCore Forked from enyim/EnyimMemcached A Me ...

  10. python jieba分词小说与词频统计

    1.知识点 """ 1)cut() a) codecs.open() 解决编码问题 b) f.readline() 读取一行,也可以使用f.readlines()读取多行 ...