zoukankan      html  css  js  c++  java
  • Entity Framework Core Code First 项目实践

    Entity Framework Core Code First 实践

    任何一种技术的出现都是为了解决一系列特定的问题,只有了解了技术所要解决的关键问题,才能理解它的真正用途,之后,才能在实践中用好它。

    Entity Framework Core也不列外,它的出现就是为了解决一个问题。在数据库的世界里面,关系型数据库从出现到现在一直是主流。而在面向对象编程的软件开发中 (Objects Oriented Programming),我们使用的频率最高的词是Object。那么,软件开发和数据库世界之间就存在明显的失配,怎么解决呢?我们可以使用object-relational mappers 或者简称为OR Mappers。在运用OR Mapper方面.NET比Java (NHibernate)晚了不是一点点,ADO.NET Entity Framework 就是微软开发的OR Mapper。 但是,我用的更多的是Entity Framework Core这个版本,要紧更技术的步伐,还有因为项目的需要。这些OR Mapper可以把数据库的表已经表之间的关系直接映射到Objects,让开发者不必再去手动做这些map了,哇,好省心!

    前提条件

    Windows 10 操作系统
    安装 .NET Core SDK
    执行以下命令,安装packages

    dotnet tool install --global dotnet-ef
    dotnet add package Microsoft.EntityFrameworkCore.Design

    因为,我们将会使用它所包含的.NET CLI, 你可以使用在cmd里面输入 dotnet ef 来判断是否安装成功。CLI stands for Command Line Interface.

    路径

    打开cmd窗口,路径一般要指向项目根目录,确保在这个目录下包含了所有的Migration 文件。

    常用操作命令

    更新数据库

    使用下面的命令更新数据库,每一次更新,都会把新的Migration文件中包含的更改,一起更新到数据库中,这是一个Append操作,不会覆盖已有的Migration,不会重建数据库。

    dotnet ef database update

    新建Migration

    使用下面的命令新建一个Migration,这里的migration实际上就是Entities的改动。一般建议每一个Migration只包含单一的更改。

    dotnet ef migrations add [NAME_of_MIGRATION]

    Migration 文件实例

    using System;
    using Microsoft.EntityFrameworkCore.Migrations;
    
    namespace TravelDesk.Api.Data.Migrations
    {
        public partial class TrainOptions_RemoveColumns : Migration
        {
            protected override void Up(MigrationBuilder migrationBuilder)
            {
                migrationBuilder.DropColumn(
                    name: "Conditions",
                    table: "TrainOptions");
    
                migrationBuilder.DropColumn(
                    name: "Departure",
                    table: "TrainOptions");
    
                migrationBuilder.DropColumn(
                    name: "Return",
                    table: "TrainOptions");
            }
    
            protected override void Down(MigrationBuilder migrationBuilder)
            {
                migrationBuilder.AddColumn<string>(
                    name: "Conditions",
                    table: "TrainOptions",
                    nullable: true);
    
                migrationBuilder.AddColumn<string>(
                    name: "Departure",
                    table: "TrainOptions",
                    nullable: true);
    
                migrationBuilder.AddColumn<string>(
                    name: "Return",
                    table: "TrainOptions",
                    nullable: true);
            }
        }
    }
    View Code

    Up 方法,是dotnet ef database update命令执行之后,即将要在数据库执行的更改
    Down 方法,是在执行更新到某个特定版本是,数据库的回滚所需的更改

    移除最新创建的Migration文件(或者是取消最新更改)

    dotnet ef migrations remove

    这个命令会移除最新创建的Migration文件,但它不会更新数据库。所以,如果这个更新已经被apply到了数据库中,这个操作是不能够删除数据库中的更改的。

    高级操作

    删除数据库

    dotnet ef database drop

    这个操作在项目开发中偶尔会用到,特别是出现Migration冲突,或者,发现很多更新都没有到本地数据库的时候。一般需要删除本地数据库,然后重建。

    把数据库更新到特定的Migration

    dotnet ef database update InitialCreate
    dotnet ef database update 20180904195021_InitialCreate

    以上操作会把数据库恢复到历史版本,也就是说,如果有1,2,3,4,5,6,7,8,9个migration, 在执行了一下命令之后

    dotnet ef databae update 5

    那么,5和之后的 6,7,8,9的migration都会回滚。注意,这个操作不会删除Migration文件,如果,你再次执行dotnet ef database update 操作,那么,更新会被再次应用到数据库中。

    经验

    尽量不要在Entity里面使用 new dateTime 或者 new GUID,合并代码的时候令人崩溃啊!

    参考文献

    https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet
    https://t-heiten.net/ef-core/reverting-a-migration/

  • 相关阅读:
    linux C/C++编程之库-动态库,静态库创建及使用
    类linux 系统iptables 系统初始化配置
    OS error set
    OpenWrt修改
    OpenWrt backfire trunk源码下载及编译
    OpenWrt compiles
    OpenWrt 学习网址
    nginx编译配置
    cocos2d-x中的坐标系
    SGU 231 Prime Sum 求&lt;=n内有多少对素数(a,b)使得a+b也为素数 规律题
  • 原文地址:https://www.cnblogs.com/Dannier/p/EntityFrameworkCore-CodeFirst.html
Copyright © 2011-2022 走看看