似乎无法让我的.Net核心应用程序与SQL Server在Docker容器中工作

我正在阅读亚当·弗里曼(Adam Freeman)撰写的“ASP.Net Core MVC Essential Angular”第4章。 我一直在试图让初始数据库在Docker容器中对SQL Server运行。

这是他的原始docker-compose.yml文件:

version: "3" services: database: image: "microsoft/mssql-server-linux:ctp2 0" ports: - 5100:1433 environment: - ACCEPT_EULA=Y - SA_PASSWORD=mySecret123 

当我尝试运行这个文件的应用程序时,我得到了一个错误的东西的影响:“没有find这个SQL Server的清单”所以我改变了:

 image: "microsoft/mssql-server-linux:ctp2 0" 

 image: "microsoft/mssql-server-linux:latest" 

并运行:

 docker-compose up 

似乎工作。

但现在我仍然得到:

 SqlException: Cannot open database "SportsStore" requested by the login. The login failed. Login faild for 'sa' 

这是其余的设置。

appsettings.json

 { "Logging": { "IncludeScopes": false, "LogLevel": { "Microsoft.EntityFrameworkCore": "Information", "Microsoft.AspNetCore.NodeServices": "Information", "Default": "Warning" } }, "Data": { "Products": { "ConnectionString": "Server=localhost,5100;Database=SportsStore;User Id=sa;Password=mySecret123;MultipleActiveResultSets=true" } } } 

的DataContext:

 using Microsoft.EntityFrameworkCore; namespace SportsStore.Models { public class DataContext : DbContext { public DataContext(DbContextOptions<DataContext> opts) : base(opts) { } public DbSet<Product> Products { get; set; } public DbSet<Supplier> Suppliers { get; set; } public DbSet<Rating> Ratings { get; set; } } } 

StartUp.cs:

 using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.AspNetCore.SpaServices.Webpack; using SportsStore.Models; using Microsoft.EntityFrameworkCore; namespace SportsStore { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddDbContext<DataContext>(options => options.UseSqlServer(Configuration["Data:Products:ConnectionString"])); services.AddMvc(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, DataContext context) { app.UseDeveloperExceptionPage(); app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions { HotModuleReplacement = true }); app.UseStaticFiles(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); SeedData.SeedDatabase(context); } } } 

一旦一切就绪,我运行:

 dotnet ef migrations add Initial 

那么这本书没有我们运行dotnet ef migrations更新。 我们下一次运行:

 docker-compose up 

启动数据库。 然后运行应用程序,我们应该看到一些播种的结果。

但是相反,我得到了'sa'login失败的错误。

任何人都知道这里发生了什么?

原来它确实需要一个

 dotnet ef migrations update 

之后

 docker-compose up 

并在运行应用程序之前生成数据库并播种。

你必须看看更新GitHub for Core 2.0中的清单