这是我的登录功能:
LoginViewModel lvm = new LoginViewModel
{
UserName = Input.UserName,
Password = Input.Password,
RememberMe = Input.RememberMe
};
var valid = accountsData.Authenticate(lvm);
if (valid != null)
{
var avm = applicationUsersData.GetByUsername(Input.UserName);
var user = applicationUsersData.Get(avm.Id);
var roles = userRolesData.GetUserRoles(user.Id);
claims.Add(new Claim(ManagementStudio.ClassLibrary.Globals.CookieGlobals.UserName, user.UserName));
claims.Add(new Claim(ManagementStudio.ClassLibrary.Globals.CookieGlobals.Email, user.Email));
claims.Add(new Claim(ManagementStudio.ClassLibrary.Globals.CookieGlobals.PhoneNumber, user.PhoneNumber));
claims.Add(new Claim(ManagementStudio.ClassLibrary.Globals.CookieGlobals.FirstName, user.FirstName));
claims.Add(new Claim(ManagementStudio.ClassLibrary.Globals.CookieGlobals.LastName, user.LastName));
claims.Add(new Claim(ManagementStudio.ClassLibrary.Globals.CookieGlobals.Id, user.Id));
foreach (var item in roles)
{
var currentItem = new UserRoleDetailsViewModel
{
Id = item.Id,
Name = item.Name,
ApplicationId = item.ApplicationId,
ApplicationName = item.ApplicationName
};
var convertedItem = JsonConvert.SerializeObject(currentItem);
claims.Add(new Claim(ManagementStudio.ClassLibrary.Globals.CookieGlobals.Roles, convertedItem));
}
await _customClaimsCookieSignInHelper.SignInUserAsync(user, Input.RememberMe, claims);
activityLogger.Log(HttpContext, user.Id, configuration[DSCASGlobals.DS_Name], configuration[DSCASGlobals.DS_Id], Input.UserName + " logged in");
return LocalRedirect(returnUrl);
}
accountsData.Authenticate位于一个称为ManagementStudio.Data的引用的dll程序集中,如下所示:
[AllowAnonymous]
public ApplicationUsers Authenticate(LoginViewModel Input)
{
PasswordHasher<ApplicationUsers> passwordHasher = new PasswordHasher<ApplicationUsers>();
ApplicationUsers user = new ApplicationUsers();
try
{
user = dbContext.ApplicationUsers.SingleOrDefault(u => u.Email == Input.UserName);
if(user != null)
{
if (passwordHasher.VerifyHashedPassword(user,user.PasswordHash,Input.Password) == PasswordVerificationResult.Success)
{
return user;
}
else
{
return null;
}
}
}
catch(Exception e)
{
logger.LogError(e, LoggingGlobals.Error);
return null;
}
return user;
}
运行应用程序并登录时,出现此错误:
遍历查询的结果时发生异常 上下文类型为“ ManagementStudio.Data.Models.ManagementStudioDbContext”。 System.Data.SqlClient.SqlException(0x80131904):无效的对象名称 “ ApplicationUsers”。在 System.Data.SqlClient.SqlConnection.OnError(SqlException异常, 布尔值breakConnection,操作为
1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction),位于 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,布尔值调用者HasConnectionLock,布尔值asyncClose) System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler,SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,Boolean&dataReady) System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()在 System.Data.SqlClient.SqlDataReader.get_MetaData()在 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior,字符串resetOptionsString)在 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior,runBehavior,布尔returnStream,布尔 异步,Int32超时,任务和任务,布尔asyncWrite,SqlDataReader ds) System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔值returnStream, TaskCompletionSource1 completion, Int32 timeout, Task& task, Boolean asyncWrite, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior) at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.ExecuteReader() at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary
2 parameterValues) Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection 连接,IReadOnlyDictionary2 parameterValues) at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable
1.Enumerator.BufferlessMoveNext(DbContext _,布尔缓冲区),位于Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute [TState,TResult](TState 状态,函数3 operation, Func
3验证成功) Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.MoveNext() at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable
1 来源) Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ResultEnumerable1.GetEnumerator() at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider._TrackEntities[TOut,TIn](IEnumerable
1 结果,QueryContext queryContext,IList1 entityTrackingInfos, IList
1个entityAccessors)+ MoveNext()位于 Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor1.EnumeratorExceptionInterceptor.MoveNext() at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable
1个源, Boolean&找到),位于System.Linq.Enumerable.First [TSource](IEnumerable1 source) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass15_1
1.b__0(QueryContext qc)ClientConnectionId:7c19efa6-d951-4139-8135-8dbcd5050c20错误 编号:208,状态:1,类别:16
ApplicationUsers是我的IdentityUser,位于ManagementStudio.Data.Models中。 DocumentStudio引用了它。
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;
}
}
}
我该如何解决这个问题?
编辑:
我的完整数据库集
public DbSet<ApplicationUsers> ApplicationUsers { get; set; }
public DbSet<UserRoles> UserRoles { get; set; }
public DbSet<IdentityUserRole<string>> IdentityUserRole { get; set; }
public DbSet<IdentityUserClaim<string>> IdentityUserClaim { get; set; }
public DbSet<Applications> Applications { get; set; }
public DbSet<Roles> Roles { get; set; }
public DbSet<ApiAccess> ApiAccess { get; set; }
public DbSet<EventLogs> EventLogs { get; set; }
public DbSet<ActivityLogs> ActivityLogs { get; set; }
public DbSet<CommunicationLogs> CommunicationLogs { get; set; }
public DbSet<UploadLogs> UploadLogs { get; set; }
public DbSet<Repositories> Repositories { get; set; }
public DbSet<Emails> Emails { get; set; }
public DbSet<Assets> Assets { get; set; }
public DbSet<Announcements> Announcements { get; set; }
public DbSet<AnnouncementAttachments> AnnouncementAttachments { get; set; }
答案 0 :(得分:0)
正如您在评论中所说,ApplicationUsers
类的表名是ManagementStudio.ApplicationUsers
。您需要告诉EF您的架构名称,因为默认情况下它使用dbo
。有很多方法可以做到这一点:
[Table("ApplicationUsers", Schema = "ManagementStudio")]
public class ApplicationUsers : IdentityUser
{
}
使用流畅的API:
public class ManagementStudioDbContext: IdentityDbContext
{
public DbSet<ApplicationUsers> ApplicationUsers { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.ToTable("ApplicationUsers", schema: "ManagementStudio");
}
}
使用流畅的API设置默认架构名称:
public class ManagementStudioDbContext: IdentityDbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("MyCustomSchema");
}
}