1

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. 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.

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

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

        public override void Down()
        {
        }
    }
}

2 Answers2

1

First You need to do Add-Migration -> It something like commit. Example: Add-Migration AddedNewFieldInTablePerson Later You need use: Update-Database

TomekPle
  • 32
  • 2
  • what i notice when i tried to run that command not all my table definition from the database do run. See the screen shot as attached what i want as both Update-Database and Scaffold-DbContext ..... -Outdir Models – mailer testmail May 25 '20 at 12:43
  • In another words it run an empty migration with data or table-definition at all. Yet if i do run scaffold command i get the exception. The one i post here – mailer testmail May 25 '20 at 12:55
  • Hm tell me first what approach You choice? Code first or Db first? – TomekPle May 25 '20 at 13:54
0

This info changes everything. The Update-Database command you are using in case of code first db. 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] []

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

If connection is configured correctly the command should create db context and models for You.

Good example of using EF to perform this operation

TomekPle
  • 32
  • 2