[英]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.