ASP.NET Core中使用EF Core(MySql)Code First
⒈添加依赖
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的更多相关文章
- 在ASP.NET Core中通过EF Core实现一个简单的全局过滤查询
前言 不知道大家是否和我有同样的问题: 一般在数据库的设计阶段,会制定一些默认的规则,其中有一条硬性规定就是一定不要对任何表中的数据执行delete硬删除操作,因为每条数据对我们来说都是有用的,并且是 ...
- ASP.NET Core 中使用EF Core 将实体映射到数据库表的方法(SQL Server)
前段时间听过一个关于使用ASP.NET Core建立项目的视频.其中使用EF Core映射到数据库的部分是按部就班地学习.今天自己建立项目时,有些步骤已经有一些遗忘.所以写下这篇文章,顺便理清思路. ...
- ASP.NET Core中使用EF Core(MySql)Database First
⒈创建数据库,在数据中执行以下脚本. CREATE DATABASE Blogging; USE Blogging; CREATE TABLE Blog ( BlogId int not null P ...
- .NET Core中使用EF Core连接MySQL
最近一直在捣鼓.NET Core方面的东西,顺便写下点东西记录下 1.打开vs2017,新建一个项目 2.vs会自动生成一个项目,然后打开NuGet搜索MySql.Data.EntityFramewo ...
- .net core 中使用ef 访问mysql
1.参考文档说修改项目文件添加,就得这么做,不然会报错 <ItemGroup> <DotNetCliToolReference Include="Microsoft.Ent ...
- Asp.net core下利用EF core实现从数据实现多租户(1)
前言 随着互联网的的高速发展,大多数的公司由于一开始使用的传统的硬件/软件架构,导致在业务不断发展的同时,系统也逐渐地逼近传统结构的极限. 于是,系统也急需进行结构上的升级换代. 在服务端,系统的I/ ...
- Asp.net core下利用EF core实现从数据实现多租户(3): 按Schema分离 附加:EF Migration 操作
前言 前段时间写了EF core实现多租户的文章,实现了根据数据库,数据表进行多租户数据隔离. 今天开始写按照Schema分离的文章. 其实还有一种,是通过在数据表内添加一个字段做多租户的,但是这种模 ...
- 万字长文,带你彻底理解EF Core5的运行机制,让你成为团队中的EF Core专家
在EF Core 5中,有很多方式可以窥察工作流程中发生的事情,并与该信息进行交互.这些功能点包括日志记录,拦截,事件处理程序和一些超酷的最新出现的调试功能.EF团队甚至从Entity Framewo ...
- [翻译 EF Core in Action 1.10] 应该在项目中使用EF Core吗?
Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Cor ...
随机推荐
- CUDA-F-2-2-核函数计时
Abstract: 本文介绍CUDA核函数计时方法 Keywords: gettimeofday,nvprof 开篇废话 继续更新CUDA,同时概率和数学分析也在更新,欢迎大家访问www.face2a ...
- cygwin执行.py提示找不到模块,但已经安装模块的解决办法
. 在解决了cygwin中make命令不能使用的问题之后(https://www.cnblogs.com/zhenggege/p/10724122.html),make maskrcnn路径下的set ...
- 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> ...
- list元素按照日期排序
private static void ListSort(List<AppClassInfoVo> list) { Collections.sort(list, new Comparato ...
- Android RecyclerView与ListView比较
RecyclerView 概述 RecyclerView 集成自 ViewGroup .RecyclerView是Android-support-V7版本中新增的一个Widgets,官方对于它的介绍是 ...
- Movidius的深度学习入门
1.Ubuntu虚拟机上安装NC SDK cd /home/shine/Downloads/ mkdir NC_SDK git clone https://github.com/movidius/nc ...
- [SQL Server常用系统存储过程大全]
1. sp_help 报告有关数据库对象(sys.sysobjects 兼容视图中列出的所有对象) sp_help 表名称,存储过程名称等 2. sp_helpdb 报告有关数据库 ...
- win10搜索框突然不能使用了
备忘: win10搜索不出来了,使用以下方法恢复了,备忘下 1,首先打开任务管理器 重新启动wservice服务 2.发现这时候搜索依然不能使用 然后重新启动explorer.exe (1)右键关闭该 ...
- GitHub-Tech-DotNet:Cnblogs
ylbtech-GitHub-Tech-DotNet:Cnblogs 1.返回顶部 · EnyimMemcachedCore Forked from enyim/EnyimMemcached A Me ...
- python jieba分词小说与词频统计
1.知识点 """ 1)cut() a) codecs.open() 解决编码问题 b) f.readline() 读取一行,也可以使用f.readlines()读取多行 ...