【问题标题】:How do i update-database using Add-Migration on Nuget command?如何在 Nuget 命令上使用 Add-Migration 更新数据库?
【发布时间】:2020-09-12 02:06:58
【问题描述】:

每次我尝试使用我的 Nuget 包控制台执行 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.

enter image description here

// 添加-迁移类对象。

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

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

        public override void Down()
        {
        }
    }
}

【问题讨论】:

    标签: c# sql-server asp.net-mvc nuget-package group-policy


    【解决方案1】:

    首先你需要做 Add-Migration -> 它类似于提交。 示例:Add-Migration addedNewFieldInTablePerson 稍后您需要使用:Update-Database

    【讨论】:

    • 当我尝试运行该命令时,我注意到并非所有来自数据库的表定义都运行。请参阅附加的屏幕截图作为更新数据库和脚手架-DbContext ..... -Outdir 模型
    • 换句话说,它运行一个带有数据或表定义的空迁移。但是,如果我确实运行了脚手架命令,则会出现异常。我在这里发布的那个
    • 嗯先告诉我你选择什么方法?代码优先还是数据库优先?
    【解决方案2】:

    这些信息改变了一切。 您在代码优先数据库的情况下使用的 Update-Database 命令。 要从现有数据库中检索数据库上下文,您需要使用以下完整命令:

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

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

    如果连接配置正确,该命令应该为您创建数据库上下文和模型。

    Good example of using EF to perform this operation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-26
      • 1970-01-01
      • 2016-11-04
      • 2020-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-16
      相关资源
      最近更新 更多