EF Core: Update-Database command not creating user tables
Solution 1
The solution was eventually simple to resolve for me using Visual Studio 2017, using Microsoft.AspNetCore.All 2.0.5.. I wasn't receiving any error messages I just had to change this in my appsettings.json
. I had to change "Server=(localdb)\\mssqllocaldb;
" to this: "Server=Localhost;
." Then I ran the command, "Update-Database
" and it created the tables.
Solution 2
When running commands to add migrations and update database you are referencing BloggingContext and you are showing us a snapshot of ApplicationDbContext. ApplicationDbContext comes with Individual User Accounts template that you have chosen, so if you want Identity tables (User table is one of them) you should extend ApplicationDbContext instead of creating a new one.
Related videos on Youtube
nam
Updated on June 04, 2022Comments
-
nam over 1 year
Following this tutorial I created an
ASP.NET Core 1.1.1 - Code first
app onVS2017 ver 15.3.3
. The only difference between the tutorial and my app is that I choseIndividual User Accounts
option and I'm usingSQL Server 2012 EXPRESS
Db instead of LocalDb. Following commands run fine and do create Blogging database with Blogs and Posts tables. Note: I've not upgraded to .NET Core 2.0 yet on both the computers for compatibility issues with some of our existing applications.PM> Add-Migration myFirstMigration -context BloggingContext PM> Update-Database -context BloggingContext
ISSUE
But when I run the following command to generate user tables (ASPNETUsers, ASPNETRols etc) the command runs without any error but the user tables are not created. This was not happening before I upgraded VS2017 to ver 15.3.3. Also, on my other computer (Windows 10 with SQLEXPRESS 2014) I don't have such a problem. This computer is Windows 7. What could possibly be the issue here - and what I may be missing here?
Update-Database -context BloggingContext
.csproj file
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>netcoreapp1.1</TargetFramework> <PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback> <UserSecretsId>aspnet-ForgotPassword-B181AA40-BA34-4A36-A650-38857D8E8177</UserSecretsId> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" /> <PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.2" /> <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="1.1.2" /> <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.1.2" /> <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" /> <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.2" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.2" PrivateAssets="All" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.2" PrivateAssets="All" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.1" PrivateAssets="All" /> <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.2" /> <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" /> <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.1" PrivateAssets="All" /> <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.2" /> </ItemGroup> <ItemGroup> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.1" /> <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="1.0.1" /> <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" /> </ItemGroup> </Project>
ApplicationDbContextModelShapshot.cs
[DbContext(typeof(ApplicationDbContext))] partial class ApplicationDbContextModelSnapshot : ModelSnapshot { protected override void BuildModel(ModelBuilder modelBuilder) { modelBuilder .HasAnnotation("ProductVersion", "1.0.0-rc3") .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); modelBuilder.Entity("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole", b => { b.Property<string>("Id"); b.Property<string>("ConcurrencyStamp") .IsConcurrencyToken(); b.Property<string>("Name") .HasAnnotation("MaxLength", 256); b.Property<string>("NormalizedName") .HasAnnotation("MaxLength", 256); b.HasKey("Id"); b.HasIndex("NormalizedName") .HasName("RoleNameIndex"); b.ToTable("AspNetRoles"); }); modelBuilder.Entity("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRoleClaim<string>", b => { b.Property<int>("Id") .ValueGeneratedOnAdd(); b.Property<string>("ClaimType"); b.Property<string>("ClaimValue"); b.Property<string>("RoleId") .IsRequired(); b.HasKey("Id"); b.HasIndex("RoleId"); b.ToTable("AspNetRoleClaims"); }); modelBuilder.Entity("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserClaim<string>", b => { b.Property<int>("Id") .ValueGeneratedOnAdd(); b.Property<string>("ClaimType"); b.Property<string>("ClaimValue"); b.Property<string>("UserId") .IsRequired(); b.HasKey("Id"); b.HasIndex("UserId"); b.ToTable("AspNetUserClaims"); }); modelBuilder.Entity("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserLogin<string>", b => { b.Property<string>("LoginProvider"); b.Property<string>("ProviderKey"); b.Property<string>("ProviderDisplayName"); b.Property<string>("UserId") .IsRequired(); b.HasKey("LoginProvider", "ProviderKey"); b.HasIndex("UserId"); b.ToTable("AspNetUserLogins"); }); modelBuilder.Entity("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserRole<string>", b => { b.Property<string>("UserId"); b.Property<string>("RoleId"); b.HasKey("UserId", "RoleId"); b.HasIndex("RoleId"); b.HasIndex("UserId"); b.ToTable("AspNetUserRoles"); }); modelBuilder.Entity("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserToken<string>", b => { b.Property<string>("UserId"); b.Property<string>("LoginProvider"); b.Property<string>("Name"); b.Property<string>("Value"); b.HasKey("UserId", "LoginProvider", "Name"); b.ToTable("AspNetUserTokens"); }); modelBuilder.Entity("ForgotPassword.Models.ApplicationUser", b => { b.Property<string>("Id"); b.Property<int>("AccessFailedCount"); b.Property<string>("ConcurrencyStamp") .IsConcurrencyToken(); b.Property<string>("Email") .HasAnnotation("MaxLength", 256); b.Property<bool>("EmailConfirmed"); b.Property<bool>("LockoutEnabled"); b.Property<DateTimeOffset?>("LockoutEnd"); b.Property<string>("NormalizedEmail") .HasAnnotation("MaxLength", 256); b.Property<string>("NormalizedUserName") .HasAnnotation("MaxLength", 256); b.Property<string>("PasswordHash"); b.Property<string>("PhoneNumber"); b.Property<bool>("PhoneNumberConfirmed"); b.Property<string>("SecurityStamp"); b.Property<bool>("TwoFactorEnabled"); b.Property<string>("UserName") .HasAnnotation("MaxLength", 256); b.HasKey("Id"); b.HasIndex("NormalizedEmail") .HasName("EmailIndex"); b.HasIndex("NormalizedUserName") .IsUnique() .HasName("UserNameIndex"); b.ToTable("AspNetUsers"); }); modelBuilder.Entity("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRoleClaim<string>", b => { b.HasOne("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole") .WithMany("Claims") .HasForeignKey("RoleId") .OnDelete(DeleteBehavior.Cascade); }); modelBuilder.Entity("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserClaim<string>", b => { b.HasOne("ForgotPassword.Models.ApplicationUser") .WithMany("Claims") .HasForeignKey("UserId") .OnDelete(DeleteBehavior.Cascade); }); modelBuilder.Entity("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserLogin<string>", b => { b.HasOne("ForgotPassword.Models.ApplicationUser") .WithMany("Logins") .HasForeignKey("UserId") .OnDelete(DeleteBehavior.Cascade); }); modelBuilder.Entity("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserRole<string>", b => { b.HasOne("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole") .WithMany("Users") .HasForeignKey("RoleId") .OnDelete(DeleteBehavior.Cascade); b.HasOne("ForgotPassword.Models.ApplicationUser") .WithMany("Roles") .HasForeignKey("UserId") .OnDelete(DeleteBehavior.Cascade); }); } }
UPDATE
I tried the above test by selecting ASP.NET Core 2.0 template on another computer that has
.NET Core 2.0
andVS2017 ver 15.3.3
and everything worked fine as expected - with same SQL Server versionSQLEXPRESS2012
. Blogging Db was created with Blogs and Posts tables and user tables ASPNETUsers, ASPNETRoles etc. So problem seems to be when you have .NET Core 1.1.1 on your system withVS2017 ver 15.3.3
and your are trying to create an ASP.NET Core 1.1.1 web app withIndividual User Accounts
thenApplicationDbContext
does not work.-
Peter about 6 yearsSince the command is running without error, can you try running the command outside of Visual Studio from your project root?
dotnet ef migrations add myFirstMigration --verbose
if that works without error, you should be able to rundotnet ef database update --verbose --context BloggingContext
-
nam about 6 years@Peter The issue is on ApplicationDbContext.
PM> update-database works fine
onBloggingContext
. From outside Visual Studio when I randotnet ef migrations add myFirstMigration --verbose
it, as expected, complained that there are two DbContext specify the one you need. So I specified--context ApplicationDbContext
attribute value and it ran fine. Then I randotnet ef database update --verbose --context ApplicationDbContext
that also ran with success. But still no user tables in Bloggings database. -
Peter about 6 yearsI forgot to mention the last command. Running
dotnet ef database update
will trigger actual table updates.
-
-
Dev almost 4 yearsThis was the only one that worked from all over the internet! Thanks!