当模型更改时,WebAPI 2中的EF标识停止工作

时间:2015-09-07 07:21:00

标签: c# angularjs entity-framework asp.net-web-api identity

我想在Angular中使用Android / iOS可以使用的Web服务构建一个网站。我终于找到了一个很棒的教程,帮助我做好准备。下一步是扩展身份模型。所以,我添加了一个属性。然后我开始得到一个例外:

"支持' 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

我已经在Startup中更改了我的初始化程序(评论显示它是什么)

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

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

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

我不知所措,我用Google搜索,但我看到的解决方案似乎并不奏效。 VS以管理员身份运行。我只是希望EF不关心我的数据库,并相信我应该如此......

实际上,如果我告诉它更换我的数据库(我不喜欢),我会收到此错误:

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

没有任何道路可以让我独自一人并相信我管理我自己的数据库......

由于

1 个答案:

答案 0 :(得分:0)

如果存在安全限制以防止装配组件,则会出现此异常和HResult代码。

典型原因:

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