美文网首页
EntityFramework Core 数据初始化和迁移

EntityFramework Core 数据初始化和迁移

作者: 满脸胡渣的年轻大叔 | 来源:发表于2023-11-02 13:54 被阅读0次

EntityFramework Core 以下简称 EF

使用VS 自带的工具

下载 EntityFramework Core Tool

先使用Nuget包管理工具,搜索并下载EntityFramework Core Tool


image.png

搜索并下载安装(各种同意就好了)

image.png

使用包管理工具,调用指令

前提: 代码中先配置好,要迁移的内容,举例可以参考尾部

  • add-migration [名称] 生成数据迁移指令
  • update-database 执行未执行的迁移指令
  • Drop-Database 删除数据库
  • ... 更多参见文档

使用命令行工具(跨平台通用mac,windows)

安装 dotnet-ef

  • 全局安装 dotnet-ef 命令行工具

      dotnet tool install --global dotnet-ef
    
image.png

执行命令

我们可以先看看有哪些指令

image.png

可以看到有 添加,打包执行,列出可用,移除,具体有使用的时候可以百度或参考文档,我这边目前也没有更多应用,先把课程中展现的内容复制出来

  • 生成迁移文件
// 这里添加一个 【init】 为此次迁移的名称
dotnet ef migrations add init
  • 数据库更新
dotnet ef database update

原理

image.png

由于, ef core tool 每次migrations 会生成对应的 migrations 文件,一旦执行update ,就像向数据库中生成对应记录。

在执行 database update 时,会向数据库中对比,migrations 文件夹中对应的每个 迁移文件是否有执行,这里类似如java 项目的 flyway

参考代码

从Entity 初始化表结构

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace xxx.API.Models
{
    public class TouristRoute
    {
        //  指定主键
        [Key]
        public Guid Id { get; set; }
        [Required]
        [MaxLength(100)]
        public string Title { get; set; }
        [Required]
        [MaxLength(1500)]
        public string Description { get; set; }
        [Column(TypeName ="decimal(18,2")]
        public decimal OriginalPrice { get; set; }
        [Range(0.0,1.0)]
        public double? DiscountPresent { get; set; }
        
        public DateTime CreateTime { get; set; }

        public DateTime? UpdateTime { get; set;}

        public DateTime? DePartureTime { get; set;}
        [MaxLength]
        public string Features { get; set; }
        [MaxLength]
        public string Fees { get; set; }
        [MaxLength]
        public string Note   { get; set; }
        /// <summary>
        /// 一对多
        /// </summary>
        public ICollection<TouristRoutePictures> TouristRoutePictures { get; set; } = new List<TouristRoutePictures>();
    }
}

从json初始化数据

注:以下代码出自慕课.net core 开发店商api

    public class AppDbContext:DbContext
    {
       // *** 省略无关代码

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            var touristRoutesJsonData = File.ReadAllText(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"/Database/touristRoutesMockData.json");
            IList<TouristRoute> touristRoutes = JsonConvert.DeserializeObject<IList<TouristRoute>>(touristRoutesJsonData);
            modelBuilder.Entity<TouristRoute>().HasData(touristRoutes);

            var touristRoutePicturesData = File.ReadAllText(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"/Database/touristRoutePicturesMockData.json");
            IList<TouristRoute> touristRoutesPicture = JsonConvert.DeserializeObject<IList<TouristRoute>>(touristRoutePicturesData);
            modelBuilder.Entity<TouristRoutePictures>().HasData(touristRoutesPicture);

            base.OnModelCreating(modelBuilder);

        }
    }

相关文章

网友评论

      本文标题:EntityFramework Core 数据初始化和迁移

      本文链接:https://www.haomeiwen.com/subject/nhxnidtx.html