.net core 实现 data first的相关操作(一)

.net core 实现 data first的相关操作

先看一下demo目录

.net core 实现 data first的相关操作(一)_第1张图片

一:准备操作:

1、先新建三个类:具有一对多的关系,添加了导航属性(可以不加)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication1.Data
{
    public enum Grade
    {
        A, B, C, D, F
    }
    public class Enrollment
    {
        public int EnrollmentID { get; set; }
        public int CourseID { get; set; }
        public int StudentID { get; set; }
        public Grade? Grade { get; set; }
        public Course Course { get; set; }
        public Student Student { get; set; }
    }
}

    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication1.Data
{
    public class Student
    {
        public int ID { get; set; }
        public string LastName { get; set; }
        public string FirstMidName { get; set; }
        public DateTime EnrollmentDate { get; set; }
        public ICollection Enrollments { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication1.Data
{
    public class Course
    {
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int CourseID { get; set; }
        public string Title { get; set; }
        public int Credits { get; set; }
        public ICollection Enrollments { get; set; }
    }
}

2、新建TestDbContext.cs(数据上下文)

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace WebApplication1.Data
{
    public class TestDbContext:DbContext
    {
        public TestDbContext(DbContextOptions options) : base(options)
        {

        }
        public DbSet Uesrs { get; set; }
        public DbSet Student { get; set; }
        public DbSet Enrollment { get; set; }
        public DbSet Course { get; set; }
    }
}

3、配置appsettings.json连接字符串(ConnectionStrings)

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "SqlServer": "Data Source=PC-20160722VLKH\\SQL2014;Initial Catalog=ZjTestDataBase;User Id=zhangjian;Password=123;"
  }
}

4、在startup.cs中添加如下代码

----------------------------------------------

            var connection = Configuration.GetConnectionString("SqlServer");
            services.AddDbContext(options =>
                options.UseSqlServer(connection, b => b.MigrationsAssembly("WebApplication1")));
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

------------------------------------------------

  public void ConfigureServices(IServiceCollection services)
        {
            services.Configure(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            var connection = Configuration.GetConnectionString("SqlServer");
            services.AddDbContext(options =>
                options.UseSqlServer(connection, b => b.MigrationsAssembly("WebApplication1")));
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

5、右键你的web工程,编辑webapplication.csproj文件如下:

新增:Version为EntityFrameworkCore的版本,可以去negut中查看

 
      Version="2.1.1" />
 

最终如下:



  
    netcoreapp2.1
  

  
    
    
    
    
  

  
    
  


到这边为止,前期工作已经做完了,现在我们开始下一步:

二、生成到数据库操作

1、主要是两个命令:

dotnet ef migrations add v1   这个命令的作用的是:当我添加了类,就要改变v1成vn,相当于版本,这样下面的命令才能知道实体被修改了,才能更新。如果实体修改了,这边不修改,讲不能更新。

注意点:要cd到项目的文件夹再执行,如图所示:

.net core 实现 data first的相关操作(一)_第2张图片

 如果不报错再执行下面的命令。

dotnet ef database update   这个命令的作用的是:把修改的更新到指定到数据库。

注意点,不要使用sa,新建一个用户,给与他创建数据库的能力,如图:

.net core 实现 data first的相关操作(一)_第3张图片

这样就做完了。

最终效果图如下:对比一下,这就是code first.

.net core 实现 data first的相关操作(一)_第4张图片

.net core 实现 data first的相关操作(一)_第5张图片

后面将继续更新.net core学习,待续。。。有疑问联系我:qq:1057359832

你可能感兴趣的:(.net,core,code,first)