每个模型的手动迁移

时间:2019-05-29 00:51:37

标签: c# asp.net entity-framework entity-framework-6

我尝试为每个模型创建手动迁移,而不是为所有模型创建1个迁移。

我已经在Migrations文件夹中创建了2个迁移类,其中包含我的模型信息(通常是因为我更喜欢这样做),

- Migrations
    - 20190528221051_CreateContactTable.cs
    - 20190528221051_CreateNoteTable.cs

20190528221051_CreateContactTable.cs


using System;
using Microsoft.EntityFrameworkCore.Migrations;

namespace api.Migrations
{
    public partial class CreateContactTable : Migration
    {
        /// <summary>
        /// Create the contacts table
        /// </summary>
        /// <param name="migrationBuilder"></param>
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Contact",
                columns: table => new {
                    Id = table.Column<int>()
                       .Annotation("Sqlite:Autoincrement", true),
                    Name = table.Column<string>(),
                    Email = table.Column<string>(nullable: true),
                    Phone = table.Column<string>(nullable: true),
                    CreatedAt = table.Column<DateTime>(),
                    UpdatedAt = table.Column<DateTime>(),
                    DeletedAt = table.Column<DateTime>()
                },
                constraints: table => { table.PrimaryKey("PK_Contact", x => x.Id); });
        }

        /// <summary>
        /// Drop the contacts table
        /// </summary>
        /// <param name="migrationBuilder"></param>
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "Contact");
        }
    }
}

20190528221051_CreateNoteTable.cs

using System;
using Microsoft.EntityFrameworkCore.Migrations;

namespace api.Migrations
{
    public class CreateNoteTable : Migration
    {
        /// <summary>
        /// Create the note table
        /// </summary>
        /// <param name="migrationBuilder"></param>
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Note",
                columns: table => new {
                    Id = table.Column<int>()
                       .Annotation("Sqlite:Autoincrement", true),
                    Title     = table.Column<string>(nullable: true),
                    Body      = table.Column<string>(nullable: true),
                    CreatedAt = table.Column<DateTime>(),
                    UpdatedAt = table.Column<DateTime>(),
                    DeletedAt = table.Column<DateTime>()
                },
                constraints: table => { table.PrimaryKey("PK_Note", x => x.Id); });
        }

        /// <summary>
        /// Drop the note table
        /// </summary>
        /// <param name="migrationBuilder"></param>
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "Note");
        }
    }
}

现在,当我运行dotnet ef migration update时,它并没有运行迁移,我只会收到以下消息:

  

未应用任何迁移。数据库已经是最新的。

我要这样做的原因是,这样我的迁移可以像这样:

{timestamp}_CreateModelTable
{timestamp}_CreateModelTable
{timestamp}_CreateModelTable
{timestamp}_Add{Row}ToModelTable
{timestamp}_Add{Row}ToModelTable
{timestamp}_Remove{Row}FromModelTable
{timestamp}_Remove{Row}FromModelTable

那么,我该如何手写自己的迁移并实际运行实体框架?我尝试搜索以下内容:

  

模型实体框架的单独迁移

     

迁移不会运行实体框架

     

在没有模型快照实体框架的情况下运行迁移

但是无济于事,所以我想在这里问。

请注意:我来自Laravel(PHP),该框架的迁移运行了没有快照的文件夹中的所有迁移,因此我不确定EF是否以相同或不同的方式进行操作,因此这可能或不可能。

0 个答案:

没有答案