3

This is the error I get when I attempt to register an account on my local ASP.NET MVC project. This all started when I tried to move the default account manager over to my main DbContext. It worked fine while it was still on "DefaultConnection."

Exception Details: System.Data.SqlClient.SqlException: Invalid column name 'UserName'.
Invalid column name 'UserName'.
Invalid column name 'Email'.
Invalid column name 'EmailConfirmed'.
Invalid column name 'PasswordHash'.
Invalid column name 'SecurityStamp'.
Invalid column name 'PhoneNumber'.
Invalid column name 'PhoneNumberConfirmed'.
Invalid column name 'TwoFactorEnabled'.
Invalid column name 'LockoutEndDateUtc'.
Invalid column name 'LockoutEnabled'.
Invalid column name 'AccessFailedCount'.
Invalid column name 'UserName'.

Source of the error is this statement from the default AccountController:

var result = await UserManager.CreateAsync(user, model.Password);

This is what my tables look like. I've been fiddling for a long time; IdentityUser was not always there, and now it seems it has "taken" AspNetUsers's column names... very odd.

enter image description here

This is my DbContext file

public class MainContext : IdentityDbContext<ApplicationUser>
    {
        public MainContext() : base("MainContext")
        {
            Database.SetInitializer(new DropCreateDatabaseAlways<MainContext>());
        }

        public DbSet<Story> Stories { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

            base.OnModelCreating( modelBuilder );
            modelBuilder.Entity<IdentityUser>().HasKey( t => t.Id );
            modelBuilder.Entity<IdentityUserLogin>().HasKey( l => new {l.UserId, l.ProviderKey, l.LoginProvider} );
            modelBuilder.Entity<IdentityRole>().HasKey( r => r.Id );
            modelBuilder.Entity<IdentityUserRole>().HasKey( r => new { r.RoleId, r.UserId } );

        }

    public DbSet<ViewModels.StoryDetailsVm> StoryViewModels { get; set; }

    public DbSet<ViewModels.StoryEditVm> StoryEditVMs { get; set; }
}

This is my IdentityModels file

public class ApplicationUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("MainContext", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }

I've been trying to solve this for 6 hours. Would greatly appreciate any help I can get. Apologies for all this code, I have no idea where the problem lies.

Stack trace

[SqlException (0x80131904): Invalid column name 'UserName'.
Invalid column name 'UserName'.
Invalid column name 'Email'.
Invalid column name 'EmailConfirmed'.
Invalid column name 'PasswordHash'.
Invalid column name 'SecurityStamp'.
Invalid column name 'PhoneNumber'.
Invalid column name 'PhoneNumberConfirmed'.
Invalid column name 'TwoFactorEnabled'.
Invalid column name 'LockoutEndDateUtc'.
Invalid column name 'LockoutEnabled'.
Invalid column name 'AccessFailedCount'.
Invalid column name 'UserName'.]
   System.Data.SqlClient.<>c.<ExecuteDbDataReaderAsync>b__167_0(Task`1 result) +870314
   System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke() +77
   System.Threading.Tasks.Task.Execute() +49
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Data.Entity.Core.EntityClient.Internal.<ExecuteStoreCommandsAsync>d__c.MoveNext() +249

[EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details.]
   System.Data.Entity.Core.EntityClient.Internal.<ExecuteStoreCommandsAsync>d__c.MoveNext() +334
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Data.Entity.Core.Objects.Internal.<ExecuteAsync>d__0`1.MoveNext() +1515
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Data.Entity.Utilities.CultureAwaiter`1.GetResult() +59
   System.Data.Entity.Core.Objects.<ExecuteInTransactionAsync>d__3d`1.MoveNext() +862
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Data.Entity.SqlServer.<ExecuteAsyncImplementation>d__9`1.MoveNext() +358
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Data.Entity.Utilities.CultureAwaiter`1.GetResult() +59
   System.Data.Entity.Core.Objects.<GetResultsAsync>d__e.MoveNext() +632
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Data.Entity.Utilities.CultureAwaiter`1.GetResult() +59
   System.Data.Entity.Internal.<FirstMoveNextAsync>d__0.MoveNext() +261
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Data.Entity.Infrastructure.<FirstOrDefaultAsync>d__25`1.MoveNext() +349
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +28
   Microsoft.AspNet.Identity.CultureAwaiter`1.GetResult() +75
   Microsoft.AspNet.Identity.EntityFramework.<GetUserAggregateAsync>d__6c.MoveNext() +682
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +28
   Microsoft.AspNet.Identity.CultureAwaiter`1.GetResult() +75
   Microsoft.AspNet.Identity.<ValidateUserName>d__4.MoveNext() +607
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Runtime.CompilerServices.TaskAwaiter.GetResult() +26
   Microsoft.AspNet.Identity.CultureAwaiter.GetResult() +44
   Microsoft.AspNet.Identity.<ValidateAsync>d__0.MoveNext() +235
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +28
   Microsoft.AspNet.Identity.CultureAwaiter`1.GetResult() +75
   Microsoft.AspNet.Identity.<CreateAsync>d__0.MoveNext() +422
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +28
   Microsoft.AspNet.Identity.CultureAwaiter`1.GetResult() +75
   Microsoft.AspNet.Identity.<CreateAsync>d__d.MoveNext() +475
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +28
   fcon.Controllers.<Register>d__15.MoveNext() in C:\Users\G\Documents\Visual Studio 2015\Projects\fcon\fcon\Controllers\AccountController.cs:157
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Runtime.CompilerServices.TaskAwaiter.GetResult() +26
   System.Threading.Tasks.TaskHelpersExtensions.ThrowIfFaulted(Task task) +42
   System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +73
   System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) +37
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +27
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +68
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +34
   System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +69
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +230
   System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +27
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +27
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +68
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +42
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +124
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +27
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +30
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +29
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +27
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +48
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +30
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +21
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +32
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +26
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +40
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +30
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +21
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +24
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +27
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +48
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +58
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +30
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +21
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +29
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +23
   System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +129

Keywords for Google: DropCreateDatabaseAlways Manually Delete Database Old Model

KSHMR
  • 741
  • 1
  • 9
  • 24
  • What does your table look like in SSMS/Query Analyzer? SQL Server is telling you those column names are invalid (sorry if that's obvious). At least, that's where I'd start. – aardvark Jan 05 '16 at 23:00

1 Answers1

0

I found the actual solution. This is one of the most infuriating bugs ever. Entity seems to leave behind a shadow copy of a manually-deleted database. The fact that my columns would simply not behave puzzled me greatly -- since I had DropCreateDatabaseAlways enabled -- and I highly suspect it was because Entity was referring to the shadow copy all the time.

This is the same reason why this bug occurs. Entity can't attach to a database with the same name and in the same location as an old databsae if the old one has been deleted -- because the old one is still there.

The solution: Download SSMS, find the shadow copy, and erase it. If you manually delete the database even once, then there is room for complications.

Community
  • 1
  • 1
KSHMR
  • 741
  • 1
  • 9
  • 24