简体   繁体   English

EF Core:Update-Database命令未创建用户表

[英]EF Core: Update-Database command not creating user tables

Following this tutorial I created an ASP.NET Core 1.1.1 - Code first app on VS2017 ver 15.3.3 . 遵循本教程,我在VS2017 ver 15.3.3上创建了ASP.NET Core 1.1.1 - Code first VS2017 ver 15.3.3 ASP.NET Core 1.1.1 - Code first应用程序。 The only difference between the tutorial and my app is that I chose Individual User Accounts option and I'm using SQL Server 2012 EXPRESS Db instead of LocalDb. 本教程和我的应用程序之间的唯一区别是,我选择了“ Individual User Accounts选项,并且我使用的是SQL Server 2012 EXPRESS Db而不是LocalDb。 Following commands run fine and do create Blogging database with Blogs and Posts tables. 以下命令运行良好,并且确实使用Blogs和Posts表创建Blogging数据库。 Note: I've not upgraded to .NET Core 2.0 yet on both the computers for compatibility issues with some of our existing applications. 注意:由于两款计算机与某些现有应用程序的兼容性问题,我尚未在两台计算机上都升级到.NET Core 2.0。

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. 但是,当我运行以下命令来生成用户表(ASPNETUsers,ASPNETRols等)时,该命令运行时没有任何错误,但未创建用户表。 This was not happening before I upgraded VS2017 to ver 15.3.3. 在我将VS2017升级到版本15.3.3之前没有发生这种情况。 Also, on my other computer (Windows 10 with SQLEXPRESS 2014) I don't have such a problem. 另外,在我的另一台计算机(带有SQLEXPRESS 2014的Windows 10)上,我也没有这样的问题。 This computer is Windows 7. What could possibly be the issue here - and what I may be missing here? 这台计算机是Windows7。这里可能是问题所在,而这里我可能缺少什么?

Update-Database -context BloggingContext

.csproj file .csproj文件

<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 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 UPDATE

I tried the above test by selecting ASP.NET Core 2.0 template on another computer that has .NET Core 2.0 and VS2017 ver 15.3.3 and everything worked fine as expected - with same SQL Server version SQLEXPRESS2012 . 我通过在另一台具有.NET Core 2.0VS2017 ver 15.3.3计算机上选择ASP.NET Core 2.0模板来尝试上述测试,并且一切运行正常,并且使用相同的SQL Server版本SQLEXPRESS2012 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 with VS2017 ver 15.3.3 and your are trying to create an ASP.NET Core 1.1.1 web app with Individual User Accounts then ApplicationDbContext does not work. Blogging Db是使用Blogs和Posts表以及用户表ASPNETUsers,ASPNETRoles等创建的。因此,问题似乎是当您在使用VS2017 ver 15.3.3系统上具有.NET Core 1.1.1时,并且您正在尝试创建ASP.NET。带有Individual User Accounts Core 1.1.1 Web应用ApplicationDbContext无法Individual User Accounts ApplicationDbContext

When running commands to add migrations and update database you are referencing BloggingContext and you are showing us a snapshot of ApplicationDbContext. 在运行添加迁移和更新数据库的命令时,您引用的是BloggingContext,并且向我们显示了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. ApplicationDbContext带有您选择的“个人用户帐户”模板,因此,如果要使用身份表(用户表是其中之一),则应扩展ApplicationDbContext而不是创建一个新表。

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 . 该解决方案最终使用Visual Studio 2017(使用Microsoft.AspNetCore.All 2.0.5。)对我来说很容易解决。我没有收到任何错误消息,只需要在appsettings.json进行更改appsettings.json I had to change " Server=(localdb)\\\\mssqllocaldb; " to this: " Server=Localhost; ." 我必须将“ Server=(localdb)\\\\mssqllocaldb; ”更改为:“ Server=Localhost; Then I ran the command, " Update-Database " and it created the tables. 然后,我运行命令“ Update-Database ”,它创建了表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM