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

 1 using Microsoft.EntityFrameworkCore;
 2 using System.Collections.Generic;
 3 using System.ComponentModel.DataAnnotations;
 4 
 5 namespace CodeFist.Models
 6 {
 7     public class BloggingContext : DbContext
 8     {
 9         public BloggingContext(DbContextOptions<BloggingContext> options)
10             : base(options)
11         { }
12         public DbSet<Blog> Blogs { get; set; }
13         public DbSet<Post> Posts { get; set; }
14     }
15     public class Blog
16     {
17         [Key]
18         public int BlogId { get; set; }
19         [MaxLength(128)]
20         public string name {get;set;}
21         [MaxLength(128)]
22         public string Author {get;set;}
23         [MaxLength(200)]
24         public string Url { get; set; }
25 
26 public List<Post> Posts { get; set; }
27     }
28     public class Post
29     {
30         [Key]
31         public int PostId { get; set; }
32         [MaxLength(50)]
33         public string Title { get; set; }
34         [MaxLength(200)]
35         public string Content { get; set; }
36         public int BlogId { get; set; }
37         public Blog Blog { get; set; }
38     }
View Code

 5, 打开Startup 类,按照如下修改
   添加引用:

    


            
using CodeFist.Models;
using Microsoft.EntityFrameworkCore;

1  public void ConfigureServices(IServiceCollection services)
2         {
3             services.AddMvc();
4             string connection =@"server=.;Initial Catalog=dcytest121;User ID=sa;Password=123;Persist Security Info=True";
5             services.AddDbContext<BloggingContext>(options => options.UseSqlServer(connection));
6      }
View Code

 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

 

转载于:https://www.cnblogs.com/duchyaiai/p/8392775.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值