簡體   English   中英

如何在 Nuget 命令上使用 Add-Migration 更新數據庫?

[英]How do i update-database using Add-Migration on Nuget command?

每次我嘗試使用我的 Nuget Package 控制台來執行 Scaffold-DbContext 時,我都會遇到這個異常並且似乎無法通過它。 請幫我解決它。

Update-Database
& : File C:\Users\gcobanim\source\repos\eNtsaRegistrationTraining\packages\Microsoft.EntityFrameworkCore.Tools.3.1.3\tools\init.ps1 cannot be loaded because its operation is blocked by software restriction policies, such as 
those created by using Group Policy.
At line:1 char:45
+ ... rgs+=$_}; & 'C:\Users\gcobanim\source\repos\eNtsaRegistrationTraining ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
No pending explicit migrations.
Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
You can use the Add-Migration command to write the pending model changes to a code-based migration.

在此處輸入圖像描述

// 添加遷移 class 對象。

namespace eNtsaRegistrationTraining.Migrations
{
    using System;
    using System.Data.Entity.Migrations;

    public partial class eNtsaRegistration2020 : DbMigration
    {
        public override void Up()
        {
        }

        public override void Down()
        {
        }
    }
}

首先,您需要執行 Add-Migration -> 它類似於提交。 示例:Add-Migration addedNewFieldInTablePerson 稍后您需要使用:Update-Database

這個信息改變了一切。 您在代碼優先數據庫的情況下使用的 Update-Database 命令。 要從現有數據庫中檢索數據庫上下文,您需要使用以下完整命令:

Scaffold-DbContext [-Connection] [-Provider] [-OutputDir] [-Context] [-Schemas>] [-Tables>] [-DataAnnotations] [-Force] [-Project] [-StartupProject] []

示例:PM> Scaffold-DbContext "Server=.\SQLExpress;Database=SchoolDB;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir 模型

如果連接配置正確,該命令應該為您創建數據庫上下文和模型。

使用 EF 執行此操作的好例子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM