2017-09-12 6 views
0

Nach this tutorial habe ich eine ASP.NET Core 1.1.1 - Code first App auf VS2017 ver 15.3.3 erstellt. Der einzige Unterschied zwischen dem Tutorial und meiner App ist, dass ich Individual User Accounts Option auswähle und ich SQL Server 2012 EXPRESS Db anstelle von LocalDb verwende. Die folgenden Befehle funktionieren einwandfrei und erstellen Blogging-Datenbanken mit Blogs und Posts-Tabellen. Hinweis: Ich habe auf beiden Computern noch kein Upgrade auf .NET Core 2.0 durchgeführt, um Kompatibilitätsprobleme mit einigen unserer vorhandenen Anwendungen zu vermeiden.EF Core: Update-Datenbank-Befehl erstellt keine Benutzertabellen

PM> Add-Migration myFirstMigration -context BloggingContext 
PM> Update-Database -context BloggingContext 

AUSGABE

Aber wenn ich den folgenden Befehl ausführen Benutzertabellen (ASPNETUsers, ASPNETRols usw.) der Befehl ausgeführt wird, ohne Fehler, aber die Benutzertabellen werden nicht erstellt, zu erzeugen. Dies geschah nicht, bevor ich VS2017 auf 15.3.3 aktualisiert habe. Auch auf meinem anderen Computer (Windows 10 mit SQLEXPRESS 2014) habe ich kein solches Problem. Dieser Computer ist Windows 7. Was könnte hier das Problem sein - und was ich hier vielleicht vermisse?

Update-Database -context BloggingContext 

CSPROJ Datei

<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

Ich habe den obigen Test versucht, indem ich die ASP.NET Core 2.0-Vorlage auf einem anderen Computer mit .NET Core 2.0 und VS2017 ver 15.3.3 auswählte und alles wie erwartet funktionierte - mit der gleichen SQL Server-Version SQLEXPRESS2012. Blogging Db wurde mit Blogs und Tabellen erstellt und Benutzer Tabellen ASPNETUsers, ASPNETRoles usw. So scheint das Problem, wenn Sie .NET Core 1.1.1 auf Ihrem System mit VS2017 ver 15.3.3 haben und versuchen, einen ASP.NET Core 1.1.1 zu erstellen Web App mit Individual User Accounts dann ApplicationDbContext funktioniert nicht.

+0

Da der Befehl ohne Fehler ausgeführt wird, können Sie versuchen, den Befehl außerhalb von Visual Studio von Ihrem Projektstamm auszuführen? 'dotnet ef Migrationen hinzufügen myFirstMigration --verbose' wenn das ohne Fehler funktioniert, sollten Sie in der Lage sein' dotnet ef database update --verbose --context BloggingContext' – Peter

+0

@Peter Das Problem ist auf ApplicationDbContext. 'PM> update-database funktioniert gut auf' BloggingContext'. Von außerhalb von Visual Studio, als ich 'dotnet ef migrations führte hinzufügen myFirstMigration --verbose' es, wie erwartet, beschwerte sich, dass es zwei DbContext geben Sie die eine, die Sie benötigen. Also habe ich den Attributwert "--context ApplicationDbContext" angegeben und es lief gut. Dann habe ich 'dotnet ef Datenbankupdate --verbose --context ApplicationDbContext' ausgeführt, das auch mit Erfolg lief. Aber noch keine Benutzer Tabellen in Blogging-Datenbank. – nam

+0

Ich habe vergessen, den letzten Befehl zu erwähnen. Durch das Ausführen von 'dotnet ef database update' werden aktuelle Tabellenaktualisierungen ausgelöst. – Peter

Antwort

1

Beim Ausführen von Befehlen zum Hinzufügen von Migrationen und Aktualisieren der Datenbank verweisen Sie auf BloggingContext und Sie zeigen uns einen Snapshot von ApplicationDbContext. ApplicationDbContext wird mit der von Ihnen gewählten Vorlage für individuelle Benutzerkonten geliefert. Wenn Sie also Identity-Tabellen verwenden möchten (die Benutzertabelle ist eine davon), sollten Sie ApplicationDbContext erweitern, anstatt eine neue zu erstellen.