c 创建mysql实体模型_EntityFrameworkCore 根据实体类自动创建数据库
1.首先新建 Asp.Net Core WebApi 项目
2.添加一下引用 :
2.1 Pomelo.EntityFrameworkCore.MySql(我用的Mysql 根据自己情况引用就行)
2.2 Microsoft.EntityFrameworkCore
2.3 Microsoft.EntityFrameworkCore.Design
3.使项目支持dotnet ef 工具以使用Migrations
3.1 手动修改csproj文件(手动添加是因为在nuget添加Microsoft.EntityFrameworkCore.Tools.DotNet 时报错,估计是vs的问题),添加一下配置
4.打开CMD命令 cd到项目目录下(C:\Users\Administrator\source\repos\CodeFirst\CodeFirst),执行
dotnet build
Microsoft (R) Build Engine version 15.1.545.13942 Copyright (C) Microsoft Corporation. All rights reserved. Startup.cs(45,13): warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. [C:\WorkSpacesC\DotNetCore\EntityFrameworkCoreMigrationsDemo\EntityFrameworkCoreMigrationsDemo\EntityFrameworkCoreMigrationsDemo.csproj] EntityFrameworkCoreMigrationsDemo -> C:\WorkSpacesC\DotNetCore\EntityFrameworkCoreMigrationsDemo\EntityFrameworkCoreMigrationsDemo\bin\Debug\netcoreapp1.0\EntityFrameworkCoreMigrationsDemo.dll Build succeeded. Startup.cs(45,13): warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. [C:\WorkSpacesC\DotNetCore\EntityFrameworkCoreMigrationsDemo\EntityFrameworkCoreMigrationsDemo\EntityFrameworkCoreMigrationsDemo.csproj] 1 Warning(s) 0 Error(s) Time Elapsed 00:00:04.76
5. 第4步完成后继续执行
dotnet ef
可以看见独角兽就说明引用成功了。距离胜利更近一步了
6.创建实例 StudentDbContext和相关实体类
usingCodeFirst.Model.Entity;usingMicrosoft.EntityFrameworkCore;usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceCodeFirst.Model.Db
{public classStudentDbContext: DbContext
{public StudentDbContext(DbContextOptionsoptions)
:base(options)
{
}protected override voidOnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMySql(@"Server=localhost;Port=3306;Database=Policy;UId=root;Pwd=mysql.com");
}public DbSet Student { get; set; }public DbSet Teacher { get; set; }
}
}
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel.DataAnnotations;usingSystem.Text;namespaceCodeFirst.Model.Entity
{public classStudent
{
[Key]public int Id { get; set; }public string Name { get; set; }public Teacher Teach { get; set; }
}
}
usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceCodeFirst.Model.Entity
{public classTeacher
{public int Id { get; set; }public int Age { get; set; }public List Stus { get; set; }
}
}
7. 在Startup将StudentDbContext 注册为服务
8.使用Migrations 新建数据库初始化类DbInitializer
usingCodeFirst.Model.Db;usingMicrosoft.EntityFrameworkCore;usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Threading.Tasks;namespaceCodeFirst.Model
{public classDbInitializer
{public voidInitializeAsync(StudentDbContext context)
{//var migrations = await context.Database.GetPendingMigrationsAsync();//获取未应用的Migrations,不必要,MigrateAsync方法会自动处理
context.Database.MigrateAsync();//根据Migrations修改/创建数据库
}
}
}
9.在Startup.cs的Configure方法中,添加参数StudentContext context,netcore会使用DI将DbContext注入到Configure方法,并添加对DbInitializer的调用。
10.使用dotnet ef命令创建Migrations:dotnet ef migrations add text text随便起名
C:\Users\Administrator\source\repos\CodeFirst\CodeFirst>dotnet ef migrations add Initial
Build succeeded.0Warning(s)0Error(s)
Time Elapsed00:00:02.32Done. To undothis action, use 'ef migrations remove'
如果执行dotnet ef migrations add Initial报错 :
解决办法执行下面
定位到csproject
PM> dotnet ef migrations script --verbose -i --project "C:\Users\Administrator\source\repos\CodeFirst\CodeFirst"
完了继续操作就行
PM>add-migration test
PM>update-database
生成数据库表:
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
修改Student实体类加一个Sex字段
public string Sex { get; set; }
执行 (注意执行命令之前在控制台‘默认项目’列表选中DbContext所在的类库,不然爆错)
Add-Migration ModifyStudent
再执行
PM> Update-Database
数据库已经更新
Demo源码: Github
c 创建mysql实体模型_EntityFrameworkCore 根据实体类自动创建数据库相关推荐
- c 自动生成mysql表结构_EntityFrameworkCore 根据实体类自动创建数据库
1.首先新建 Asp.Net Core WebApi 项目 2.添加一下引用 : 2.1 Pomelo.EntityFrameworkCore.MySql(我用的Mysql 根据自己情况引用就行) ...
- java实体类生成mysql表_springboot+mybatis通过实体类自动生成数据库表的方法
前言 本章介绍使用mybatis结合mysql数据库自动根据实体类生成相关的数据库表. 首先引入相关的pom包我这里使用的是springboot2.1.8.RELEASE的版本 org.mybatis ...
- Springboot+Maven+Mybatis-enhance-actable 根据实体类自动更新数据库表和字段
参考文章springboot+mybatis/mybatis-plus根据实体类自动创建数据库表,我在上面加了需要修改的说明 目录 pom.xml application.yml DataSource ...
- Spring Data Jpa 实体类自动创建数据库表失败解决
先说一下我遇到的这个问题,首先我是通过maven创建了一个spring boot的工程,引入了Spring data jpa,结果实体类创建好之后,运行工程却没有在数据库中自动创建数据表. 找了半天发 ...
- 实体类自动创建数据库表失败解决
先说一下我遇到的这个问题,首先我是通过maven创建了一个spring boot的工程,引入了Spring data jpa,结果实体类创建好之后,运行工程却没有在数据库中自动创建数据表. 找了半天发 ...
- Hibrenate实现根据实体类自动创建表或添加字段
Hibernate支持自动建表,在开发阶段很方便,可以保证hbm与数据库表结构的自动同步. 实现: 在配置hibernate的配置文件中将hbm2ddl.auto设置为update,如:Xml代码&l ...
- Do You Kown Asp.Net Core - 根据实体类自动创建Razor Page CURD页面模板
Scaffolding Template Intro 我们知道在Asp.Net MVC中,如果你使用的EF的DBContext的话,你可以在vs中通过右键解决方案-添加控制器-添加包含视图的控制器,然 ...
- 实战SSM_O2O商铺_02数据模型设计及实体类的创建
文章目录 模块介绍 建立o2o数据库(MySql) 数据模型及对象设计 总览 区域 分析 实体类 数据库表 用户信息 分析 实体类 数据库表 微信账号与本地账号 分析 微信账号-实体类 微信账号-数据 ...
- 根据java实体类生成创建表sql步骤
根据java实体类生成创建表sql步骤 根据java实体类生成创建表sql语句时,方法是利用java反射+AOP注解,主要步骤如下: 1.注解类 一般在生成表的时候,需要表名.主键名.字段名,对应到注 ...
最新文章
- 深度学习调参体验(一)
- RecyclerView 刷新的时候出现阴影的处理方法
- R可视化多元线性回归模型
- 依赖注入框架Autofac学习笔记
- 性能监控工具 NewRelic 简介
- TP5.1查询用Db('不含表前缀')/Db::name('不含表前缀')/Db::table('含表前缀')返回数组;model()返回对象
- 安卓创建快捷方式相关问题 Intent Intent-filter
- 2020年,我来盘点下微服务架构技术栈
- Linux进程间通信(四) - 共享内存
- php怎么表示合数,什么是合数 合数的定义
- backtrack-回溯搜索算法总结
- 用轻量服务器搭建自己的pdf在线工具箱(支持pdf压缩以及pdf OCR)
- Java正则表达式简单入门
- 山东女子学院数据科学与计算机学院官网,数据科学与计算机学院举办2006级校友返校活动...
- 海外出货量占比超七成,海外市场决定小米手机的未来
- 人的一生七八十年,到底该如何度过?
- Chrome访问https页面 攻击者可能会试图从 XX.XX.XX.XX 窃取您的信息(例如:密码、通讯内容或信用卡信息)直接键盘敲入这11个字符:thisisunsafe
- 王川:小米盒子背后的人
- 海信电视黑屏出现android,海信智能电视开机黑屏原因和解决办法
- python 的fcntl模块
热门文章
- 史上最全异常检测算法概述
- 我应该如何处理MySQL中的--secure-file-priv?
- iOS 8 UITableView分隔符插入0不起作用
- .on(#39;click#39;)与.click()之间的区别
- 如何删除内联块元素之间的空间?
- 监控MySQL数据库的主从状态的shell脚本
- k8s 关键字以及管理流程。
- 【Python】卸载完Python3 之后 Python2 无法打开IDLE
- 混合云是企业IT的未来吗?
- 分类算法之朴素贝叶斯分类(Naive Bayesian classification)