Identity and EF (Startup.cs, DbSetInitializer, and EnsureCreateAsync)
I've combed Identity and EntityFramework repositories for answers and found some test and sample code but the don't do a good job of showcasing the functionality from Startup.cs point of view.
So, what's the purpose of the DbSetInitializer if its never called? What is the correct way to ensure my database is created calling SqlServerDatabase.EnsureCreateAsync() and ensuring my custom DbSetInitializer is executed?
DbSetInitializer has seed data that allows my application to function.
DbSetInitializer is for initializing the DbSet<T> properties on a DbContext, it has nothing to do with database initialization.
In EF7 we've done away with the concept of database initializers. We would encourage you to use migrations to create and maintain the database (you can see that workflow in action here). Alternatively, you can use DbContext.Database.EnsureCreated() but there is no way to upgrade the created database if you make model changes (aside from dropping and recreating it). DbContext.Database.EnsureCreated() is really only intended for use in tests etc.
BTW SqlServerDatabase isn't a type you would typically code against. It's an underlying type that EF will delegate to when you are targeting SQL Server.
Thanks for providing the clarity and I already reviewed the workflow in action before posting. I did remove the DbSetInitializer and found a solutions to my problem using a similar approached found in the Identity repository's sample application but this solutions isn't aligned with what you are proposing which is to use Migration.
I will give this a go.
One final thought, how does the Migration fit with Identity or should I be using some other authorization/authentication other than old AspNetMembership?
Sounds like the sample in the Identity repo is probably just out of date. When you create a new application in Visual Studio that uses Identity it now scaffolds an initial migration to add the identity tables and you can then continue on adding migrations if you change/extend the model at all.
That's fine. I currently developing without an IDE using only what is provided via github.com/aspnet. This means I only have K Runtime (kvm or dotnetsdk), K Package Manager (kpm), notepad++ and ILSpy. There's no scaffolding to generate any migration code etc.
My understanding is EF 7 will attempt to generate the database the first if it doesn't existing. I have logging information in my project that outputs information when OnModelCreating method is executed,
Here's what I get
PS F:\apps\pcsc\GreenShelter\src\mvc> k web
[INFO:Green Shelter] Startup => Startup
[INFORMATION:Green Shelter] Startup => Startup
[INFO:Green Shelter] Startup => Configure Services
[INFORMATION:Green Shelter] Startup => Configure Services
[INFO:Green Shelter] Startup => Configure Development
[INFORMATION:Green Shelter] Startup => Configure Development
[INFO:Green Shelter] Startup => Configure
[INFORMATION:Green Shelter] Startup => Configure
[INFO:Green Shelter] Startup => Configure Authenticiation
[INFORMATION:Green Shelter] Startup => Configure Authenticiation
Started
[INFO:Green Shelter] SpaController => StartPage
[INFORMATION:Green Shelter] SpaController => StartPage
[INFO:Green Shelter] SpaController => StartPage
[INFORMATION:Green Shelter] SpaController => StartPage
[INFO:Green Shelter] ClientController => /api/v1/client/authtypes
[INFORMATION:Green Shelter] ClientController => /api/v1/client/authtypes
[INFO:Green Shelter] /api/v1/client/login [POST] UserName: [email protected]
[INFORMATION:Green Shelter] /api/v1/client/login [POST] UserName: [email protected]
[INFO:Green Shelter] GreenShelterDbContext => Configuring DbContext
[INFORMATION:Green Shelter] GreenShelterDbContext => Configuring DbContext
[INFO:Green Shelter] GreenShelterDbContext => Creating Model for DbContext
[INFORMATION:Green Shelter] GreenShelterDbContext => Creating Model for DbContext
[ERROR:Green Shelter] Login was unsuccessful
System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or more errors occurred. ---
> System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or more errors occurred. -
--> System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or more errors occurred.
---> System.Data.SqlClient.SqlException: Invalid object name 'AspNetUsers'.
at System.Data.SqlClient.SqlCommand.<ExecuteDbDataReaderAsync>b__24(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<InitializeAndReadAsync>d__7.Move
Next()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNext>d__6.MoveNext()
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Threading.Tasks.TaskExtensions.CultureAwaiter`1.GetResult()
at Microsoft.Data.Entity.Query.TaskResultAsyncEnumerable`1.Enumerator.<MoveNext>d__3.MoveNext()
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Query.EntityQueryExecutor.AsyncEnumerableExceptionInterceptor`1.AsyncEnumeratorExceptionInte
rceptor.<MoveNext>d__6.MoveNext()
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNet.Identity.SignInManager`1.<PasswordSignInAsync>d__30.MoveNext()
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at PCSC.GreenShelter.Controllers.Api.V1.ClientController.Login(Login login) in F:\apps\pcsc\GreenShelter\src\mvc\Cont
rollers\Api\v1\ClientController.cs:line 67
---> (Inner Exception #0) System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or
more errors occurred. ---> System.AggregateException: One or more errors occurred. ---> System.AggregateException: One
or more errors occurred. ---> System.AggregateException: One or more errors occurred. ---> System.Data.SqlClient.SqlExce
ption: Invalid object name 'AspNetUsers'.
at System.Data.SqlClient.SqlCommand.<ExecuteDbDataReaderAsync>b__24(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<InitializeAndReadAsync>d__7.Move
Next()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNext>d__6.MoveNext()
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Threading.Tasks.TaskExtensions.CultureAwaiter`1.GetResult()
at Microsoft.Data.Entity.Query.TaskResultAsyncEnumerable`1.Enumerator.<MoveNext>d__3.MoveNext()
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Query.EntityQueryExecutor.AsyncEnumerableExceptionInterceptor`1.AsyncEnumeratorExceptionInte
rceptor.<MoveNext>d__6.MoveNext()
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNet.Identity.SignInManager`1.<PasswordSignInAsync>d__30.MoveNext()
---> (Inner Exception #0) System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or
more errors occurred. ---> System.AggregateException: One or more errors occurred. ---> System.AggregateException: One
or more errors occurred. ---> System.Data.SqlClient.SqlException: Invalid object name 'AspNetUsers'.
at System.Data.SqlClient.SqlCommand.<ExecuteDbDataReaderAsync>b__24(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<InitializeAndReadAsync>d__7.Move
Next()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNext>d__6.MoveNext()
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Threading.Tasks.TaskExtensions.CultureAwaiter`1.GetResult()
at Microsoft.Data.Entity.Query.TaskResultAsyncEnumerable`1.Enumerator.<MoveNext>d__3.MoveNext()
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Query.EntityQueryExecutor.AsyncEnumerableExceptionInterceptor`1.AsyncEnumeratorExceptionInte
rceptor.<MoveNext>d__6.MoveNext()
---> (Inner Exception #0) System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or
more errors occurred. ---> System.AggregateException: One or more errors occurred. ---> System.Data.SqlClient.SqlExcept
ion: Invalid object name 'AspNetUsers'.
at System.Data.SqlClient.SqlCommand.<ExecuteDbDataReaderAsync>b__24(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<InitializeAndReadAsync>d__7.Move
Next()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNext>d__6.MoveNext()
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Threading.Tasks.TaskExtensions.CultureAwaiter`1.GetResult()
at Microsoft.Data.Entity.Query.TaskResultAsyncEnumerable`1.Enumerator.<MoveNext>d__3.MoveNext()
---> (Inner Exception #0) System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or
more errors occurred. ---> System.Data.SqlClient.SqlException: Invalid object name 'AspNetUsers'.
at System.Data.SqlClient.SqlCommand.<ExecuteDbDataReaderAsync>b__24(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<InitializeAndReadAsync>d__7.Move
Next()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNext>d__6.MoveNext()
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
---> (Inner Exception #0) System.AggregateException: One or more errors occurred. ---> System.Data.SqlClient.SqlExceptio
n: Invalid object name 'AspNetUsers'.
at System.Data.SqlClient.SqlCommand.<ExecuteDbDataReaderAsync>b__24(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<InitializeAndReadAsync>d__7.Move
Next()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNext>d__6.MoveNext()
--- End of inner exception stack trace ---
---> (Inner Exception #0) System.Data.SqlClient.SqlException (0x80131904): Invalid object name 'AspNetUsers'.
at System.Data.SqlClient.SqlCommand.<ExecuteDbDataReaderAsync>b__24(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<InitializeAndReadAsync>d__7.Move
Next()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNext>d__6.MoveNext()
ClientConnectionId:c907e5ce-6765-4aee-83e7-124956886636<---
<---
<---
<---
<---
<---
[ERROR:Green Shelter] Login was unsuccessful
System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or more errors occurred. ---
> System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or more errors occurred. -
--> System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or more errors occurred.
---> System.Data.SqlClient.SqlException: Invalid object name 'AspNetUsers'.
at System.Data.SqlClient.SqlCommand.<ExecuteDbDataReaderAsync>b__24(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<InitializeAndReadAsync>d__7.Move
Next()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNext>d__6.MoveNext()
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Threading.Tasks.TaskExtensions.CultureAwaiter`1.GetResult()
at Microsoft.Data.Entity.Query.TaskResultAsyncEnumerable`1.Enumerator.<MoveNext>d__3.MoveNext()
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Query.EntityQueryExecutor.AsyncEnumerableExceptionInterceptor`1.AsyncEnumeratorExceptionInte
rceptor.<MoveNext>d__6.MoveNext()
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNet.Identity.SignInManager`1.<PasswordSignInAsync>d__30.MoveNext()
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at PCSC.GreenShelter.Controllers.Api.V1.ClientController.Login(Login login) in F:\apps\pcsc\GreenShelter\src\mvc\Cont
rollers\Api\v1\ClientController.cs:line 67
---> (Inner Exception #0) System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or
more errors occurred. ---> System.AggregateException: One or more errors occurred. ---> System.AggregateException: One
or more errors occurred. ---> System.AggregateException: One or more errors occurred. ---> System.Data.SqlClient.SqlExce
ption: Invalid object name 'AspNetUsers'.
at System.Data.SqlClient.SqlCommand.<ExecuteDbDataReaderAsync>b__24(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<InitializeAndReadAsync>d__7.Move
Next()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNext>d__6.MoveNext()
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Threading.Tasks.TaskExtensions.CultureAwaiter`1.GetResult()
at Microsoft.Data.Entity.Query.TaskResultAsyncEnumerable`1.Enumerator.<MoveNext>d__3.MoveNext()
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Query.EntityQueryExecutor.AsyncEnumerableExceptionInterceptor`1.AsyncEnumeratorExceptionInte
rceptor.<MoveNext>d__6.MoveNext()
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNet.Identity.SignInManager`1.<PasswordSignInAsync>d__30.MoveNext()
---> (Inner Exception #0) System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or
more errors occurred. ---> System.AggregateException: One or more errors occurred. ---> System.AggregateException: One
or more errors occurred. ---> System.Data.SqlClient.SqlException: Invalid object name 'AspNetUsers'.
at System.Data.SqlClient.SqlCommand.<ExecuteDbDataReaderAsync>b__24(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<InitializeAndReadAsync>d__7.Move
Next()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNext>d__6.MoveNext()
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Threading.Tasks.TaskExtensions.CultureAwaiter`1.GetResult()
at Microsoft.Data.Entity.Query.TaskResultAsyncEnumerable`1.Enumerator.<MoveNext>d__3.MoveNext()
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Query.EntityQueryExecutor.AsyncEnumerableExceptionInterceptor`1.AsyncEnumeratorExceptionInte
rceptor.<MoveNext>d__6.MoveNext()
---> (Inner Exception #0) System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or
more errors occurred. ---> System.AggregateException: One or more errors occurred. ---> System.Data.SqlClient.SqlExcept
ion: Invalid object name 'AspNetUsers'.
at System.Data.SqlClient.SqlCommand.<ExecuteDbDataReaderAsync>b__24(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<InitializeAndReadAsync>d__7.Move
Next()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNext>d__6.MoveNext()
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Threading.Tasks.TaskExtensions.CultureAwaiter`1.GetResult()
at Microsoft.Data.Entity.Query.TaskResultAsyncEnumerable`1.Enumerator.<MoveNext>d__3.MoveNext()
---> (Inner Exception #0) System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or
more errors occurred. ---> System.Data.SqlClient.SqlException: Invalid object name 'AspNetUsers'.
at System.Data.SqlClient.SqlCommand.<ExecuteDbDataReaderAsync>b__24(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<InitializeAndReadAsync>d__7.Move
Next()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNext>d__6.MoveNext()
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
---> (Inner Exception #0) System.AggregateException: One or more errors occurred. ---> System.Data.SqlClient.SqlExceptio
n: Invalid object name 'AspNetUsers'.
at System.Data.SqlClient.SqlCommand.<ExecuteDbDataReaderAsync>b__24(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<InitializeAndReadAsync>d__7.Move
Next()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNext>d__6.MoveNext()
--- End of inner exception stack trace ---
---> (Inner Exception #0) System.Data.SqlClient.SqlException (0x80131904): Invalid object name 'AspNetUsers'.
at System.Data.SqlClient.SqlCommand.<ExecuteDbDataReaderAsync>b__24(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<InitializeAndReadAsync>d__7.Move
Next()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNext>d__6.MoveNext()
ClientConnectionId:c907e5ce-6765-4aee-83e7-124956886636<---
<---
<---
<---
<---
<---
And here's my DbContext class
using System;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.OptionsModel;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Infrastructure;
using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.SqlServer;
using PCSC.GreenShelter;
using PCSC.GreenShelter.Extensions;
namespace PCSC.GreenShelter.Models {
/// <summary>
/// Provides the the data access for the Green Shelter Application
/// </summary>
public class GreenShelterDbContext: IdentityDbContext<ApplicationUser, ApplicationRole, string>, IGreenShelterApplication {
/// <summary>
/// Describe name for the class implementing <cref="IGreenShelterApplication"/> interface
/// </summary>
public string TagName { get { return "GreenShelterDbContext"; } }
public DbSet<Address> Addresses { get; set; }
public DbSet<Organization> Organizations { get; set; }
protected override void OnModelCreating(ModelBuilder builder) {
this.WriteInformation("Creating Model for DbContext");
base.OnModelCreating(builder);
builder.Entity<Address>().Key(a => a.AddressId);
builder.Entity<Organization>().Key(a => a.OrganizationId);
//builder.Entity<ApplicationUser>().ForRelational(u => u.Addresses);
//builder.Entity<ApplicationUser>().OneToMany(u => u.Organizations);
}
protected override void OnConfiguring(DbContextOptions options) {
this.WriteInformation("Configuring DbContext");
var sqlOptions = options.UseSqlServer();
}
} // end class
} // end namespace
Here's the code the causes the model configuration
[HttpPost("login")]
[AllowAnonymous]
public JsonResult Login([FromBody] Login login) {
this.WriteInformation("/api/v1/client/login [POST] UserName: {0}", login.UserName);
var response = new ApiResponse();
try {
response.Code = 200;
response.Description = "Login was successful";
// Use the following link for explaination of authenticationmethod
// https://msdn.microsoft.com/en-us/library/microsoft.identitymodel.claims.authenticationmethods_members.aspx
SignInManager.PasswordSignInAsync(login.UserName, login.Password, false, false).Wait();
} catch(Exception ex) {
response.Code = 401;
response.Description = "Login was unsuccessful";
this.WriteError(response.Description, ex);
}
return new JsonResult(response);
}
The error clearly states that AspNetUsers was not found or created. I'm also aware that duplicate console log message are display. This would result from the frameworks targeted by the project.json file. Any idea what could be wrong?
EF7 won't automatically create a database for you (this would occur in EF6.x and earlier release but time has shown that this was too much magic and causes more confusion that help).
The best way to create a database is to use migrations. Make sure you have the EntityFramework.Commands package installed and the following in your project.json:
...
"commands": {
"ef": "EntityFramework.Commands"
},
...
You can then use k ef migration add <migration name> and k ef migration apply at the command line to create a migration and apply the changes to the database.
I continue to be impressed. I was aware the commands section could be used for executing test, web application or applications but missed the fact, it also handles migration for EF. Thanks for your help.
Closing this one but feel free to reopen if you have further issues