我正在阅读Adam Freeman撰写的“ASP.Net Core MVC的基本 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
当我尝试使用此文件运行应用程序时,出现错误,影响到一些东西:“找不到此sql服务器的 list ”
所以我改变了:
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 update。
接下来,我们运行:
docker-compose up
启动数据库。
然后运行该应用程序,我们应该看到播种的结果。
但是相反,我收到“sa”的登录失败错误。
有人知道这是怎么回事吗?
最佳答案
原来它确实需要一个
dotnet ef migrations update
之后
docker-compose up
然后在运行应用程序以生成数据库并为其播种之前。
关于sql-server - 似乎无法让我的.Net Core应用程序在Docker容器中与SQL Server一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47001530/