gpt4 book ai didi

c# - 如何在包管理器中执行迁移?

转载 作者:行者123 更新时间:2023-11-30 20:26:13 25 4
gpt4 key购买 nike

我安装了 FluentMigration 来管理我的 SQL 文件。

在包管理中我执行以下命令:

PM> dotnet add package FluentMigrator
PM> dotnet add package FluentMigrator.Runner

迁移

 [Migration(201805041513)]
public class _201805041513_CriacaoTabelaPessoa : ForwardOnlyMigration
{
public override void Up()
{
Create.Table("Pessoa")
.InSchema("angularCore")
.WithColumn("Id").AsInt32().Identity()
.WithColumn("Nome").AsString(80)
.WithColumn("SobreNome").AsString(50)
.WithColumn("Email").AsString(50)
.WithColumn("IdTpoPessoa").AsInt16()
.WithColumn("IdEndereco").AsInt16();


}
}

进程外(针对某些企业需求)

PM> dotnet tool install -g FluentMigrator.DotNet.Cli

错误:

No executable found corresponding to the "dotnet-tool" command

Documentation

编辑

运行

PM> dotnet tool install -g FluentMigrator.DotNet.Cli
PM> dotnet fm migrate -p sqlite -c "Data Source=test.db" -a ".\bin\Debug\netcoreapp2.1\test.dll"

生成teste.db

enter image description here

In the old versions you run the migrations directly in the database, I did not understand how to update my database, ie create the Person table through the generated test.db file?

最佳答案

我能够毫无问题地让它工作。这是我所做的:

首先我安装了 .NET Core 2.1-Preview 2。安装后我验证了版本:

dotnet --version
2.1.300-preview2-008533

然后我创建了项目

mkdir testfm
cd testfm
dotnet new console

安装了 nuget 包

dotnet add package FluentMigrator
dotnet add package FluentMigrator.Runner
dotnet add package FluentMigrator.Runner.SQLite
dotnet add package Microsoft.Data.Sqlite

安装CLI工具

dotnet tool install -g FluentMigrator.DotNet.Cli

创建了一个名为 Migration1.cs 的迁移类

     using FluentMigrator;

namespace test
{
[Migration(201805041513)]
public class _201805041513_CriacaoTabelaPessoa : ForwardOnlyMigration
{
public override void Up()
{
Create.Table("Pessoa")
.InSchema("angularCore")
.WithColumn("Id").AsInt32().Identity()
.WithColumn("Nome").AsString(80)
.WithColumn("SobreNome").AsString(50)
.WithColumn("Email").AsString(50)
.WithColumn("IdTpoPessoa").AsInt16()
.WithColumn("IdEndereco").AsInt16();


}
}
}

编译项目

dotnet build

从根项目目录运行迁移

dotnet fm migrate -p sqlite -c "Data Source=test.db" -a ".\bin\Debug\netcoreapp2.1\test.dll"

然后我收到了以下消息。 Sucess FM Run

然后我通过查看 SqlLite 数据库确认该表已创建

SqlLite DB View

要在 Sql Server 2016 上运行相同的迁移,您需要运行:

dotnet fm migrate -p SqlServer2016 -c "server=SQLSERVERINSTANCE;uid=testfm;pwd=test;Trusted_Connection=yes;database=FluentMigrator" -a ".\bin\Debug\netcoreapp2.1\test.dll"

关于c# - 如何在包管理器中执行迁移?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50309064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com