使用Visual Studio Code,.net Core 2.0进行CodeFirst
来源:互联网 发布:江苏语音网络系统注册 编辑:程序博客网 时间:2024/05/18 15:52
使用Visual Studio Code,.net Core 2.0进行CodeFirst 开发。
(一)环境配置
Viusal Studio Code 最新版本。 .net Core 2.0.
(二):
1,打开Viusal Studio Code,在 “终端”中,使用命令 cd 定位到创建项目得盘符。这里定位到E盘。
2,使用 “mkdir” 创建文件夹。这里使用 “mkdir CodeFirst”.
3,使用donet new mvc 创建模板。
4,在Model文件下,新建Models类。
在项目中引用第三方插件:
点击 查看>命令面板>NuGet Package Manager>Microsoft.EntityFrameworkCore.SqlServer
点击 查看>命令面板>NuGet Package Manager>Microsoft.EntityFrameworkCore.Too
点击 查看>命令面板>NuGet Package Manager>Microsoft.VisualStudio.Web.CodeGeneration.Design
using Microsoft.EntityFrameworkCore;using System.Collections.Generic;using System.ComponentModel.DataAnnotations;namespace CodeFist.Models{ public class BloggingContext : DbContext { public BloggingContext(DbContextOptions<BloggingContext> options) : base(options) { } public DbSet<Blog> Blogs { get; set; } public DbSet<Post> Posts { get; set; } } public class Blog { [Key] public int BlogId { get; set; } [MaxLength(128)] public string name {get;set;} [MaxLength(128)] public string Author {get;set;} [MaxLength(200)] public string Url { get; set; }public List<Post> Posts { get; set; } } public class Post { [Key] public int PostId { get; set; } [MaxLength(50)] public string Title { get; set; } [MaxLength(200)] public string Content { get; set; } public int BlogId { get; set; } public Blog Blog { get; set; } }
5, 打开Startup 类,按照如下修改
添加引用:
using CodeFist.Models;
using Microsoft.EntityFrameworkCore;
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); string connection =@"server=.;Initial Catalog=dcytest121;User ID=sa;Password=123;Persist Security Info=True"; services.AddDbContext<BloggingContext>(options => options.UseSqlServer(connection)); } 自己按照自己本地得数据库进行修改。
6,点击打开 CodeFirst.csproj 文件。添加
<ItemGroup> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" /> <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" /> </ItemGroup>
7, dotent ef 命令是在cmd> cd E:\CodeFirst 下执行
8,为了防止要建得数据库与本地数据库冲突,执行 dotnet ef database drop 命令。
9, 执行初始创建命令 dotnet ef migrations add InitialCreate
10,执行 dotnet ef database update 数据库创建或者修改命令。
11, 如果修改了Model类,例如 添加类,或者字段。 执行 dotnet ef migrations add 修改变化版本名字(例如 addColumUserName)
然后再执行 dotnet ef database update
官方网址:
https://docs.microsoft.com/zh-cn/aspnet/core/data/ef-mvc/migrations#introduction-to-migrations
https://docs.microsoft.com/zh-cn/ef/core/get-started/aspnetcore/new-db
- 使用Visual Studio Code,.net Core 2.0进行CodeFirst
- 使用Visual Studio Code 开发.NET Core应用程序
- 【实验手册】使用Visual Studio Code 开发.NET Core应用程序
- .NET Core Visual Studio Code 环境配置
- 使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(二)-- Web Api Demo
- 使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(一)-- 起步
- 使用Visual Studio Code进行开发
- 使用 VS Code 进行 .Net core 开发
- docker4dotnet #3 在macOS上使用Visual Studio Code和Docker开发asp.net core和mysql应用
- Visual Studio Code 使用
- Visual Studio Code使用
- Visual Studio Code 使用Git进行版本控制
- Visual Studio Code 使用Git进行版本控制
- 使用visual studio code进行go语言环境搭建
- Visual Studio Code 使用Git进行版本控制
- Visual Studio Code 使用Git进行版本控制
- 使用Visual Studio Code对Node.js进行断点调试
- 用 Visual Studio Code 在 macOS 上创建首个 ASP.NET Core 应用程序
- C++ builder 管道的应用 实例 红色字体
- Python实现Pat 1023. Have Fun with Numbers (20)
- SQLServer批量插数据
- 拼写错误检测(动态规划法)
- VL6180X传感器驱动
- 使用Visual Studio Code,.net Core 2.0进行CodeFirst
- TCP/IP 5层协议
- leetcode219[easy]----Contains Duplicate II
- numpy的基本操作
- vue-router相同的地址,如何刷新数据
- Part itemtype解读(2): RelationshipType
- Android下使用Protobuf进行序列化
- 《Apache Flume教程》第四章 Flume数据库数据流
- Quartz入门