问题
When I load Package Manager Console within Visual Studio 2017 v15.9.6 in a project that uses Entity Framework I receive the following error:
\packages\EntityFramework.6.2.0\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:\Bitbucket\project-path\packages\EntityFramework.6.2. ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess
This prevents me from running commands specific to Entity Framework like "Update-Database" or "Add-Migration".
Here are things I have tried:
- Reinstall Visual Studio
- In Group Policy Editor, for both Computer Configuration and User Configuration, I have enabled the setting "Turn on Script Execution" that is located at "Administrative Templates\Windows Components\Windows PowerShell". The setting for both configurations has the "Execution Policy" set to "Allow all scripts".  
- I've tried locally in the Package Manager Console setting the "Process" scope to both "Bypass" and "Unrestricted", and then manually loading the Entity Framework init.ps1 script. I receive the same error. 
- I've also tried modifying registry keys for PowerShell to set the ExecutionPolicy to "Unrestricted" in a few places. Those places are at:
- HKLM\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell
- HKLM\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.SqlServer.Management.PowerShell.sqlps120
- HKLM\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.SqlServer.Management.PowerShell.sqlps140
 
- I've restarted multiple times in between doing all of the above.
When I run the command "Get-ExecutionPolicy -List", here are my results"
Scope         ExecutionPolicy
----- -----------------------
MachinePolicy       Undefined
UserPolicy       Unrestricted
Process             Undefined
CurrentUser      Unrestricted
LocalMachine     Unrestricted
After all of the above I expected to be able to just load Visual Studio as normal, launch Package Manager Console, and not receive any errors when it tries to run the Entity Framework init.ps1 script. I must be missing something or doing something incorrectly. What do I need to do to get Visual Studio to work as expected?
Some more information is that this is a computer joined to a company domain, but no other developers have this issue. There isn't a group policy set by an administrator that is trickling down preventing me from running scripts.
回答1:
The error may be occurring because of an invalid certificate from Microsoft. Check in Control Panel => Internet Options => Content => Certificates on the Untrusted Publishers tab. Remove Microsoft Corporation from this list.
Sounds bizarre but it worked for me.
Tks https://github.com/NuGet/Home/issues/7158
回答2:
Try downgrading to EF 6.1.2.
I ran into the same problem as you. I tried changing the Group Execution Policies which in my case didn't work, because of some restriction on my local machine (may be set by my school admin). In another project I used 6.1.2 where things like migration was no problem.
来源:https://stackoverflow.com/questions/54636948/entity-framework-powershell-script-cannot-be-loaded-by-visual-studio-because-its
