.NET Core 2.2 SqlException:无效的对象名称'ApplicationUsers'

时间:2019-06-25 13:33:52

标签: c# asp.net asp.net-mvc asp.net-core asp.net-core-mvc

我有2个名为ManagementStudio和DocumentStudio的应用程序。我已经将ManagementStudio编译成DocumentStudio引用的几个程序集。

我为两个应用程序使用相同的数据库,但是它们被不同的模式分开。例如。 ManagementStudio.ApplicationUsers和DocumentStudio.Documents

在DocumentStudio中,这是我的Startup.cs:

services.AddDbContext<DocumentStudioDbContext>(options => options.UseSqlServer(Environment.GetEnvironmentVariable(DSCASGlobals.DS_ConnectionString)));

string assemblyName = typeof(ManagementStudioDbContext).Namespace;
services.AddDbContext<ManagementStudioDbContext>(options =>
    options.UseSqlServer(Environment.GetEnvironmentVariable(DSCASGlobals.DS_ConnectionString),
        optionsBuilder =>
            optionsBuilder.MigrationsAssembly(assemblyName)
    )
);
services.AddMvc().AddJsonOptions(options =>
{
    options.SerializerSettings.ContractResolver
        = new Newtonsoft.Json.Serialization.DefaultContractResolver();

});

var lockoutOptions = new LockoutOptions()
{
    DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5),
    MaxFailedAccessAttempts = 5,
};

services.AddDefaultIdentity<ApplicationUsers>(options =>
{
    options.Lockout = lockoutOptions;
})
  .AddEntityFrameworkStores<ManagementStudioDbContext>();

DocumentStudio依靠ManagementStudio进行登录,因为ManagementStudio包含所有用户数据和IdentityUser Model。

这是DocumentStudio上的登录名,类似于ManagementStudio上的登录名:

private readonly SignInManager<ApplicationUsers> _signInManager;
private readonly UserManager<ApplicationUsers> _userManager;
private readonly ApplicationUsersData applicationUsersData;

public LoginModel(SignInManager<ApplicationUsers> signInManager, UserManager<ApplicationUsers> userManager, ApplicationUsersData applicationUsersData)
{
    _signInManager = signInManager;
    _userManager = userManager;
    this.applicationUsersData = applicationUsersData;
}

public async Task<IActionResult> OnPostAsync(string returnUrl = null){
    var result = await _signInManager.PasswordSignInAsync(Input.UserName, Input.Password, Input.RememberMe, lockoutOnFailure: true);
}

但是,由于某种原因,这是我尝试登录时遇到的错误:

enter image description here

如何处理此问题?

编辑:

添加了我的ApplicationUsers

public class ApplicationUsers : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime? DateOfBirth { get; set; }
    private DateTime createdOn = DateTime.Now;
    public DateTime CreatedOn
    {
        get
        {
            return (createdOn == DateTime.MinValue) ? DateTime.Now : createdOn;
        }
        set
        {
            createdOn = value;
        }
    }
    private DateTime updatedOn = DateTime.Now;
    public DateTime UpdatedOn
    {
        get
        {
            return (updatedOn == DateTime.MinValue) ? DateTime.Now : updatedOn;
        }
        set
        {
            updatedOn = value;
        }
    }
}

编辑2:

添加了我的DbContexts

Document Studio

public DocumentStudioDbContext(DbContextOptions<DocumentStudioDbContext> options) : base(options) { }
public DbSet<Documents> Documents { get; set; }
public DbSet<DocumentCategories> DocumentCategories { get; set; }

ManagementStudio

public ManagementStudioDbContext(DbContextOptions<ManagementStudioDbContext> options) : base(options){}
public DbSet<ApplicationUsers> ApplicationUsers { get; set; }
public DbSet<UserRoles> UserRoles { get; set; }

编辑3:

我怀疑是因为DocumentStudio在寻找自己的ApplicationUser。 ApplicationUser仅对ManagementStudio有意义。也许解决方案是重写passwordsigninasync并拥有一个调用ManagementStudios数据的密码。

0 个答案:

没有答案