ASP.Net Core:无法打开登录请求的localdb [英] ASP.Net Core : Cannot open localdb requested by the login
问题描述
提示: 处理请求时发生未处理的异常. SqlException:无法打开登录请求的数据库"MoviesContext-8287b737-12b5-4c1c-8bf5-6259ea1d208b".登录失败. 用户'D-PC \ D'登录失败.
Prompt: An unhandled exception occurred while processing the request. SqlException: Cannot open database "MoviesContext-8287b737-12b5-4c1c-8bf5-6259ea1d208b" requested by the login. The login failed. Login failed for user 'D-PC\D'.
System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity身份,SqlConnectionString connectionOptions,对象providerInfo,bool redirectedUserInstance,SqlConnectionString userConnectionOptions,SessionData reconnectSessionData,bool applyTransientFaultHandling)
System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, object providerInfo, bool redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, bool applyTransientFaultHandling)
我之前在项目目录的命令提示符中运行过以下命令(已经创建了模型并创建了自己的Dbcontext):
dotnet restore
dotnet ef migrations add Initial
dotnet ef database update
I had run the following commands in the command prompt in my project's catalog before(already made my model and made a my own Dbcontext):
dotnet restore
dotnet ef migrations add Initial
dotnet ef database update
我已经检查了Internet上的每种方法,但是所有这些方法都没有在ASP.Net Core项目( Visual Studio 2017 )中发生.通过这些方式,我找不到在何处访问SQL服务或web.config文件.
I have checked out every method on the Internet, but all of them didn't happened in the ASP.Net Core project(Visual Studio 2017). In those ways, I cannot find where to acess the SQL serve or the web.config file.
推荐答案
有必要检查是否已创建数据库,并在必要时创建 这是通过 context.Database.EnsureCreated();
It is necessary to check that the database is created and, if necessary, create This is done by the context.Database.EnsureCreated();
例如:
public static void Initialize(IServiceProvider serviceProvider)
{
ApplicationDЬContext context = serviceProvider.GetRequiredService<ApplicationDЬContext>();
if (!context.Database.EnsureCreated())
context.Database.Migrate();
if (!context.Products.Any())
{
context.Products.AddRange(
new Product
{
Name = "Soccer Ball ",
Description = "FIFA-approved size and weig",
Category = "Soccer",
Price = 19.50m
},
new Product
{
Name = "Stadium",
Description = " Flat - packed 35,000- seat stadium",
Category = "Soccer",
Price = 79500
}
);
context.SaveChanges();
}
这篇关于ASP.Net Core:无法打开登录请求的localdb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!