当模型改变时,WebAPI 2中的EF Identity停止工作

本文关键字:中的 EF Identity 停止工作 WebAPI 模型 改变 | 更新日期: 2023-09-27 18:11:34

我想用Android/iOS可以使用的web服务在Angular中构建一个网站。我终于找到了一个很棒的教程来帮助我设置。下一步是扩展身份模型。我添加了一个属性。然后我开始得到一个异常:

"支持'AuthContext'上下文的模型在数据库创建后发生了变化。考虑使用Code First Migrations来更新数据库(http://go.microsoft.com/fwlink/?LinkId=238269)。

所以,当我按照这些说明,我得到这个:

 Enable-Migrations
Exception calling "LoadFrom" with "1" argument(s): "Could not load file or assembly 'file:///C:'copa'AngularJSAuthentication-master'packages'EntityFramework.6.1.0'tools'EntityFramework.PowerShell.
Utility.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)"
At C:'copa'AngularJSAuthentication-master'packages'EntityFramework.6.1.0'tools'EntityFramework.psm1:780 char:62
+     $utilityAssembly = [System.Reflection.Assembly]::LoadFrom <<<< ((Join-Path $ToolsPath EntityFramework.PowerShell.Utility.dll))
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
You cannot call a method on a null-valued expression.
At C:'copa'AngularJSAuthentication-master'packages'EntityFramework.6.1.0'tools'EntityFramework.psm1:781 char:50
+     $dispatcher = $utilityAssembly.CreateInstance <<<< (
    + CategoryInfo          : InvalidOperation: (CreateInstance:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
Exception calling "CreateInstanceFrom" with "8" argument(s): "Could not load file or assembly 'file:///C:'copa'AngularJSAuthentication-master'packages'EntityFramework.6.1.0'tools'EntityFramework.P
owerShell.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)"
At C:'copa'AngularJSAuthentication-master'packages'EntityFramework.6.1.0'tools'EntityFramework.psm1:809 char:31
+     $domain.CreateInstanceFrom <<<< (
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
PM> Enable-Migrations -ContextTypeName AngularJSAuthentication.API.AuthContext
The project 'AngularJSAuthentication.API' failed to build.
PM> Enable-Migrations -ContextTypeName AngularJSAuthentication.API.AuthContext
Exception calling "LoadFrom" with "1" argument(s): "Could not load file or assembly 'file:///C:'copa'AngularJSAuthentication-master'packages'EntityFramework.6.1.0'tools'EntityFramework.PowerShell.
Utility.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)"
At C:'copa'AngularJSAuthentication-master'packages'EntityFramework.6.1.0'tools'EntityFramework.psm1:780 char:62
+     $utilityAssembly = [System.Reflection.Assembly]::LoadFrom <<<< ((Join-Path $ToolsPath EntityFramework.PowerShell.Utility.dll))
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
You cannot call a method on a null-valued expression.
At C:'copa'AngularJSAuthentication-master'packages'EntityFramework.6.1.0'tools'EntityFramework.psm1:781 char:50
+     $dispatcher = $utilityAssembly.CreateInstance <<<< (
    + CategoryInfo          : InvalidOperation: (CreateInstance:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
Exception calling "CreateInstanceFrom" with "8" argument(s): "Could not load file or assembly 'file:///C:'copa'AngularJSAuthentication-master'packages'EntityFramework.6.1.0'tools'EntityFramework.P
owerShell.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)"
At C:'copa'AngularJSAuthentication-master'packages'EntityFramework.6.1.0'tools'EntityFramework.psm1:809 char:31
+     $domain.CreateInstanceFrom <<<< (
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
PM> Enable-Migrations
Exception calling "LoadFrom" with "1" argument(s): "Could not load file or assembly 'file:///C:'copa'AngularJSAuthentication-master'packages'EntityFramework.6.1.0'tools'EntityFramework.PowerShell.
Utility.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)"
At C:'copa'AngularJSAuthentication-master'packages'EntityFramework.6.1.0'tools'EntityFramework.psm1:780 char:62
+     $utilityAssembly = [System.Reflection.Assembly]::LoadFrom <<<< ((Join-Path $ToolsPath EntityFramework.PowerShell.Utility.dll))
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

我在启动时更改了初始化器(注释显示了它是什么)

Database.SetInitializer<AuthContext>(null);// <AuthContext>());//, AngularJSAuthentication.API.Migrations.Configuration>());

和上下文类中的静态构造函数(我在另一个项目中就是这样做的)

static AuthContext()
    {
        Database.SetInitializer<AuthContext>(new AuthContextInitialiser());
    }

我很困惑,我搜索过了,但我看到的解决方案似乎都不起作用。VS以管理员身份运行。我只是希望EF不关心我的数据库,并相信我,这是它应该的方式....

实际上,如果我告诉它替换我的DB(我不喜欢),我得到这个错误:

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

没有一条路可以让我独自一人,相信我可以管理自己的数据库…

谢谢

当模型改变时,WebAPI 2中的EF Identity停止工作

如果存在阻止程序集加载的安全限制,则会出现此异常和HResult代码。

典型的原因:

  • 网络共享或驱动器:将文件复制到硬盘
  • 从其他机器复制的DLL:右键单击并取消阻塞