.NET core 2.0 solution in Microsoft Visual Studio 2017, with latest updates
My app hasn't worked since back when I had everything running great on .NET core 1.1. I had gotten off to a very nice start after watching some Shawn Wildermuth, Kevin Dockx and Julie Lehrman training videos on Pluralsight. Unfortunately, now things have radically changed in .NET 2.0! I originally tried to upgrade, but that was a disaster! I eventually gave up and started afresh with a .NET core 2.0 solution and with copy/paste of the old code.
My solution has two projects, one ASP.NET core app for an API and one .NET core console app for data. I'm using ASP.NET identity core with Entity Framework Core (2.0.1), and I'm connecting to an MSSQL Server 2017 SQLEXPRESS database.
As I'm starting afresh, I have no database. And so I open Package Manager and run "Add-Migration Initial". This generates the following output...
PM> Add-Migration Initial
Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
User profile is available. Using 'C:\Users\user1\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 2.0.1-rtm-125 initialized 'DatabaseContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (256ms) [Parameters=[], CommandType='Text', CommandTimeout='60']
CREATE DATABASE [CFC0000001TC];
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (58ms) [Parameters=[], CommandType='Text', CommandTimeout='60']
IF SERVERPROPERTY('EngineEdition') <> 5 EXEC(N'ALTER DATABASE [CFC0000001TC] SET READ_COMMITTED_SNAPSHOT ON;');
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (6ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [__EFMigrationsHistory] (
[MigrationId] nvarchar(150) NOT NULL,
[ProductVersion] nvarchar(32) NOT NULL,
CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
);
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT OBJECT_ID(N'__EFMigrationsHistory');
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [MigrationId], [ProductVersion]
FROM [__EFMigrationsHistory]
ORDER BY [MigrationId];
Microsoft.EntityFrameworkCore.Migrations[20405]
No migrations were applied. The database is already up to date.
fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
Failed executing DbCommand (24ms) [Parameters=[@__normalizedUserName_0='?' (Size = 256)], CommandType='Text', CommandTimeout='30']
SELECT TOP(1) [u].[Id], [u].[AccessFailedCount], [u].[ConcurrencyStamp], [u].[Email], [u].[EmailConfirmed], [u].[FirstName], [u].[LastName], [u].[LockoutEnabled], [u].[LockoutEnd], [u].[NormalizedEmail], [u]. [NormalizedUserName], [u].[PasswordHash], [u].[PhoneNumber], [u].[PhoneNumberConfirmed], [u].[SecurityStamp], [u].[TwoFactorEnabled], [u].[UserName]
FROM [SystemUser] AS [u]
WHERE [u].[NormalizedUserName] = @__normalizedUserName_0
System.Data.SqlClient.SqlException (0x80131904): Invalid object name 'SystemUser'.
at System.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__108_0(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.<ExecuteAsync>d__17.MoveNext()
ClientConnectionId:9a66b342-9ca7-427e-af3a-8cffa483c72f
Error Number:208,State:1,Class:16
fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
Failed executing DbCommand (24ms) [Parameters=[@__normalizedUserName_0='?' (Size = 256)], CommandType='Text', CommandTimeout='30']
SELECT TOP(1) [u].[Id], [u].[AccessFailedCount], [u].[ConcurrencyStamp], [u].[Email], [u].[EmailConfirmed], [u].[FirstName], [u].[LastName], [u].[LockoutEnabled], [u].[LockoutEnd], [u].[NormalizedEmail], [u]. [NormalizedUserName], [u].[PasswordHash], [u].[PhoneNumber], [u].[PhoneNumberConfirmed], [u].[SecurityStamp], [u].[TwoFactorEnabled], [u].[UserName]
FROM [SystemUser] AS [u]
WHERE [u].[NormalizedUserName] = @__normalizedUserName_0
System.Data.SqlClient.SqlException (0x80131904): Invalid object name 'SystemUser'.
at System.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__108_0(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
--- End of stack trace from previous location where exception was thrown ---
As you can see, the following error message is generated:
Invalid object name 'SystemUser'.
When I use SQL Management Studio to view the created database, I can only see one table, and it pertains to EF migrations. There are no tables relating to ASP.NET identity or anything else.
Additional info:
No "Migrations" folder is generated/created in my project
My database context class inherits from IdentityDbContext
My database context (among other items) contains these two functions:
public DatabaseContext(DbContextOptions<DatabaseContext> options) :
base(options)
{
Database.Migrate();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=MYSERVER\\SQLEXPRESS;Database=CFC0000001TC;Trusted_Connection=True;");
}
My startup class' ConfigureServices contains:
services.AddDbContext<DatabaseContext>(ServiceLifetime.Scoped);
services.AddIdentity<Data.Entities.System.SystemUser, IdentityRole>().AddEntityFrameworkStores<DatabaseContext>();
services.AddScoped<IRepository, Repository>();
services.AddTransient<DatabaseInitializer>();
My startup class' Configure contains:
app.UseAuthentication();
Also, my startup class' Configure contains (as the final line):
databaseInitializer.Seed().Wait();
If I just run the app, it seems to fail on the first line of the databaseInitializer.Seed().Wait() call, which contains a call to UserManager.FindByNameAsync(...
Can you guys please help me figure out what's wrong here? What are some possible causes for the ASP.NET identity core tables not getting generated in my database when I run "Add-Migration Initial"?