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

 

 posted on 2018-01-31 16:03  一朵茉莉花  阅读(197)  评论(0编辑  收藏  举报