简体   繁体   English

如何在 Nuget 命令上使用 Add-Migration 更新数据库?

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

Each time i am trying to use my Nuget Package Console to do Scaffold-DbContext i am getting this exception and cant seem to pass it.每次我尝试使用我的 Nuget Package 控制台来执行 Scaffold-DbContext 时,我都会遇到这个异常并且似乎无法通过它。 Please help me to resolve it.请帮我解决它。

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在此处输入图像描述

// Add-Migration class objects. // 添加迁移 class 对象。

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

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

        public override void Down()
        {
        }
    }
}

First You need to do Add-Migration -> It something like commit.首先,您需要执行 Add-Migration -> 它类似于提交。 Example: Add-Migration AddedNewFieldInTablePerson Later You need use: Update-Database示例:Add-Migration addedNewFieldInTablePerson 稍后您需要使用:Update-Database

This info changes everything.这个信息改变了一切。 The Update-Database command you are using in case of code first db.您在代码优先数据库的情况下使用的 Update-Database 命令。 To retrieve db context from existing db You need to use this full command:要从现有数据库中检索数据库上下文,您需要使用以下完整命令:

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

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

If connection is configured correctly the command should create db context and models for You.如果连接配置正确,该命令应该为您创建数据库上下文和模型。

Good example of using EF to perform this operation 使用 EF 执行此操作的好例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 之后我发生了添加迁移和更新数据库问题 - After when I made add-migration and update-database problem occur 实体框架代码优先:迁移失败,更新数据库,强制不必要的(?)添加迁移 - Entity Framework code-first: migration fails with update-database, forces unneccessary(?) add-migration 在存在Cache.insert的地方进行添加迁移或更新数据库失败 - Add-migration or update-database fails where Cache.insert exists 不使用ASP.Net Core 2中的迁移命令Update-Database创建数据库 - Not creating database using Migration command Update-Database in ASP.Net Core 2 “更新数据库”命令不会更新到最新版本的迁移 - "Update-Database" command does not update to the latest version of Migration 初始迁移后,EF6中的update-database命令不起作用 - update-database command in EF6 not working after initial migration 代码首次迁移中更新数据库命令出错 - Error in update-database command in code first migration 没有nuget包的Add-Migration是否可能? - Add-Migration without nuget package possible? 如何使用 pm> Update-Database 命令? - how to use pm> Update-Database command? 如何在使用基于代码的迁移时停止添加迁移检查我的数据库没有挂起的迁移? - How can I stop Add-Migration checking my database has no pending migrations when using Code-Based migrations?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM