MVC Asp.NET 发生身份未知错误

MVC Asp.NET Identity Unknown Error Occurring

我正在开发一个 MVC 站点,该站点使用 ASP.Net Identity 作为登录部分。

有一个用户 table,其中 pk 是另一个 table 的外键(table 2)。因此,无法删除用户。此外,还有从 table 2 生成的报告。此报告显示来自 table 2 的数据,只要生成数据的用户名即可。不应该删除用户信息的另一个原因。

因此,我不想删除用户,而是向用户 table 添加一个名为 "Deleted" 的列。它使用位数据类型。

该列是使用代码优先迁移添加到我的本地数据库副本中的。当用户登录时,此列中的此值被选中(0 = 未删除;1 = 已删除)。

在我的本地机器上一切正常。我没有错误,代码 运行 很好。我能够使用任何用户登录。然后我部署到我们的测试服务器,并使用脚本将 "Deleted" 列添加到测试 SQL 服务器上的 table。

当我用任何用户登录时,我不能。尽管我的凭据是正确的,但我一直收到 "Error logging in. Please re-enter credentials and try again"。

我删除了服务器上的文件,重新部署。一样的。

我撤消了所有更改并重新部署并能够登录。 所以,我开始一次添加一些东西:

我不明白为什么这个添加会导致问题。 "Deleted" 列在应用程序的其他 table 中使用,没有引起任何问题。因为当我在本地 运行 它工作正常时,我很难确定问题是什么。

这是我的 BL 实体:

using Microsoft.AspNet.Identity.EntityFramework;
     using System;
 using System.Collections.Generic;
namespace BlahBlah.BusinessLogic.Entities
 {
public class User : IdentityUser
{
    public User() : base() { }

    public User(string username) : base(username) { }

    public Guid CompanyId { get; set; }

    public Company Company { get; set; }

    public bool PrimaryAccount { get; set; }

    public Guid DefaultCompanyId { get; set; }

    public ICollection<Transaction> Transactions { get; set; }

   // public bool Deleted { get; set; }

}
}   

这是我的一段 user.js 代码:

...
$http(call)
            .success(function (data) {
                userData.isAuthenticated = true;
                userData.username = data.userName;
                userData.role = data.role;
                userData.bearerToken = data.access_token;
                userData.expirationDate = new Date(data['.expires']);

                setHttpAuthHeader();

                if (typeof successCallback === 'function') {
                    successCallback();
                }
            })
            .error(function (data) {

                if (typeof errorCallback === 'function') {
                    if (data.error_description) {
                        errorCallback(data.error_description);
                    }
                    else {

                        errorCallback('Unable to contact server; please, try again later.');
                    }
                }
            });
    };
... 

这是我的一段 login.js 代码:

...
 function login(common, $location, config) {
    var getLogFn = common.logger.getLogFn;
    var log = getLogFn(controllerId);

    var vm = this;

    vm.title = 'Login';
    vm.credentials = {
        username: '',
        password: ''
    };
    vm.hideError = true;
    vm.errorMessage = 'xx.';

    vm.login = function login() {
        vm.hideError = true;
        common.$broadcast(config.events.spinnerToggle, { show: true });
        common.user.authenticate(vm.credentials.username, vm.credentials.password, onSuccessfullLogin, onFailedLogin);
    };
...
function onFailedLogin(error) {
        common.$broadcast(config.events.spinnerToggle, { show: false });
        console.log('error ' + error);
        vm.hideError = false;
    }
...  

关于正在发生的事情或我如何找出正在发生的事情有什么想法吗?谢谢你的时间!

更新 我不应该太兴奋。在应用程序中执行一些测试后,我想我会测试报告。直到那时,一切都运转良好。当我查看报告时出现以下错误:

The model backing the 'BabDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database

报告正在使用 SSRS。如果我 运行 创建列的脚本并更新 _Migrations table 我不明白为什么会收到此错误。堆栈跟踪指向最后一行。我已阅读建议,但我需要在 OnModelCreating 方法中添加一些代码,但我不确定这是否可行。在我之前已经有其他人完成了其他部署,所以我遗漏了一些东西。

public class AbaDbContext : IdentityDbContext<User>, IDbContext
{
    public const int UnknownCTId = 1;
    private IEncryptionService _encryptionService;
    private static Dictionary<Guid, string> _cipherCache = new Dictionary<Guid,string>();

    public BabDbContext()
        : base("BabDbContext")
    {
        ((IObjectContextAdapter)this).ObjectContext.ObjectMaterialized += new ObjectMaterializedEventHandler(ObjectMaterialized);

您的上下文似乎正在尝试 运行 生产服务器上的迁移,但由于权限不足或 Deleted 列已存在而失败。您的选择是:

  1. 运行 通过创建脚本进行生产迁移。您可以通过 运行 执行以下命令来执行此操作:

    Update-Database -Script
    

    这将创建一个 SQL 脚本,您可以 运行 针对您的数据库创建列,并且关键的是,在 __MigrationHistory table 中插入一行告诉 EF 它不需要再次 运行 迁移。

  2. 创建一个有效禁用迁移的上下文初始化程序。添加一个继承自 NullDatabaseInitializer<T> 的 class:

    public class DisableMigrations : NullDatabaseInitializer<YourContext>
    {
    }
    

    在你的作品中web.config:

    <entityFramework>
      <contexts>
        <context type="YourNamespace.Domain.YourContext, YourNamespace.Domain">
          <databaseInitializer 
                  type="YourNamespace.Domain.DisableMigrations, YourNamespace.Domain"/>
        </context>
      </contexts>
    </entityFramework>