.NET Identity UserManager.FindAsyc throwing “Invalid column name 'Discriminator'.”

巧了我就是萌 提交于 2021-02-07 10:34:17

问题


I created a new .NET Web Application project and choose the Individual User Account Authentication for the template I selected. By default this sets up the Identity database to be a local database in the App_Data directory. This worked fine. But then I needed the AspNet tables for membership to be in my real SQL Server database that holds the rest of my application. So I changed the DefaultConnection string to point to my local SQL Server database and when I reran the application, EF automatically created those tables in my real database. So everything seemed good.

Then when I try to Login and it calls UserManager.FindAsync, it gives me the following SqlException: "Invalid column name 'Discriminator'.". The full StackTrace is below for reference.

I'm using the same ApplicationDbContext and ApplicationUser that come with the default web application templates.

Any ideas for what I am doing wrong? This is my first time working with .NET Identity. Is there a different way to make it work with an existing SQL Server database besides just changing the connection string?

Thanks.

Full Stack Trace:

   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)\r\n   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)\r\n   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)\r\n   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)\r\n   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()\r\n   at System.Data.SqlClient.SqlDataReader.get_MetaData()\r\n   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)\r\n   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)\r\n   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)\r\n   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)\r\n   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)\r\n   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)\r\n   at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)\r\n   at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<>c__DisplayClassb.<Reader>b__8()\r\n   at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TInterceptionContext,TResult](Func`1 operation, TInterceptionContext interceptionContext, Action`1 executing, Action`1 executed)\r\n   at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)\r\n   at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior)\r\n   at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)\r\n   at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)

回答1:


Two different DbContexts? The error message could be caused by a database created by a DbContext with a DbSet of type IdentityUser or maybe more likely some entity related to IdentityUser in some way. while the UserManager seems to be using another Context (the ApplicationDbContext?) containing a DbSet of a type inheriting from IdentityUser. When you have a type inheriting from another type in your model entity framework will use/expect a column called discriminator that will contain the type name of the entity that is stored in a particular row.




回答2:


This error would also happen if Self-Hosted WEB API created the Database, and then you tried to access it from ASP.NET MVC site. Both Projects come as default in VS 2013.

As Olav Nybo said, this is probably related to the fact that ASP.NET MVC tries to login as ApplicationUser that inherits from IdentityUser. The quick fix for me was to generate the database from ASP.NET MVC Web Site.




回答3:


Little late to the party but, what I saw in this is that when extending the IdentityUser, the AspNetUsers table needs your new custom columns for the attributes being used AND a column for the types of IdentityUser.

For example if you are creating a new ApplicationUser : IdentityUser then there will be a new column in the table named Discriminator and it will have a value of ApplicationUser. This is the case that you are not using Code First and you need to manually apply your database changes.



来源:https://stackoverflow.com/questions/20008827/net-identity-usermanager-findasyc-throwing-invalid-column-name-discriminator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!