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

问题描述 投票:1回答:2

每次我试图使用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.

请在这里输入图片描述

/ Add-Migration类对象。

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
2个回答
1
投票

首先,你需要做Add-Migration -> 它类似于commit.Example。Add-Migration AddedNewFieldInTablePersonLater 你需要使用: 更新-数据库


0
投票

这个信息改变了一切。如果你使用的是代码第一的数据库,你需要使用这个完整的命令来从现有的数据库中获取数据库上下文。

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 Models

如果连接配置正确,该命令应该为你创建db上下文和模型。

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

© www.soinside.com 2019 - 2024. All rights reserved.