使用Visual Studio Code,.net Core 2.0进行CodeFirst

使用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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值