EF Code First development model of

EF Code First development model of 

Code First name implies, the first code. First, complete the preparation of related entities DbContext class and the derived class, and then automatically create database tables in the database by mapping relationship.
Create a class and student management in this case, there are classes and student class MyClass class Student. Class class MyClass which can contain a number of students Student, are many references to the relationship between the two.
(If you have problems in the implementation process, you can view the final mark paper)

First, prepare the environment

  • Development Environment: .NET Core 3.1
  • IDE tools: Visual Studio 2019
  • Database: SQL Server 2012

Second, add a reference package

It requires the following package of references:
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Design
can be achieved by adding the relevant packages VS dotnet cli command line or the nuget package manager. Dotnet cli command line here to add Design bag, for example. In order to open the project root line, performing dotnet add package {} package can be cited.

example: dotnet add package Microsoft.EntityFrameworkCore.Design

Third, the program code programming

1. Entity Class Code

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace efc1.Models
{
    public class MyClass
    {
        [Key]
        [MaxLength(10)]
        public string ClassNo { get; set; }

        [MaxLength(50)]
        [Required]
        public String ClassName { get; set; }

        public  IList<Student> students { get; set; }
    }
}

``C#
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.DataAnnotations;//对Model字段进行注解需要引用该类库;
using System.ComponentModel.DataAnnotations.Schema;

namespace efc1.Models
{
public class Student
{
[Key]
[StringLength (10)]
public String StuNo { get; set; }

    public String StuName { get; set; }

    public String Gender { get; set; }

    public DateTime  Birthday { get; set; }

    //外键相关
    public String ClassNo { get; set; }//ClassNo属性,是Student类中的外键,与MyClass类相对应。

    [ForeignKey("ClassNo")]//ForeignKey特性,需引用System.ComponentModel.DataAnnotations.Schema;

    public  MyClass MyClass { get; set; }
}

}
```

2.DbContext派生类代码

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.EntityFrameworkCore;

namespace efc1
{
    public class MyDBContext:DbContext
    {
        //定义针对Student的DbSet集合;
        public DbSet<Models.Student> Students { get; set; }
        public DbSet<Models.MyClass> MyClasses { get; set; }

        //对Dbcontext进行初始化配置操作
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            string connString = "Data Source = .; Initial Catalog=text;Integrated Security=True";
            optionsBuilder.UseSqlServer(connString);//指定使用sql server提供程序,并设置连接字符串;

        }   
    }
}

四、创建迁移并更新数据库

1.通过命令行方式,在项目根目录执行命令:dotnet ef migrations add Test,创建名为Test的迁移任务(执行此步骤时需要将VS保存并关闭后,再在项目根目录执行命令);
命令执行成功后,在项目中会自动产生一个Migrations文件夹,其中自动生成的CSharp代码文件就是用于更新数据库的。

dotnet ef migrations add Test

2.继续执行命令:dotnet ef database update,根据项目中的实体映射关系,更新数据库(包括表结构、表直接的关系)。

dotnet ef database update

【注】若命令行提示“dotnet ef命令无法使用”的错误,我们可通过以下命令,先安装对应的命令工具(其中version 3.1.1,需与开发者系统中安装的.net core版本保持一致)。
dotnet tool install --global dotnet-ef --version 3.1.1

【注】如果出现以下错误请如此解决:

Guess you like

Origin www.cnblogs.com/nvyuan/p/12322772.html