简体   繁体   English

ASP.net Core Web API localdb连接字符串异常

[英]ASP.net Core Web api localdb connection string exception

I have a bit a frustating problem, whenever I run a asp.net solution with a localdb. 每当我使用localdb运行asp.net解决方案时,我都会遇到一个令人沮丧的问题。 I always get this exception. 我总是得到这个例外。

My question is, how can I fix this? 我的问题是,我该如何解决? I just want it to generate a small localdatabase where I can do CRUD operations to via REST API 我只希望它生成一个小的本地数据库,我可以在其中通过REST API进行CRUD操作

System.Data.SqlClient.SqlException: 'A network-related or instance-specific error occurred while establishing a connection to SQL Server. System.Data.SqlClient.SqlException:'与SQL Server建立连接时发生与网络相关或特定于实例的错误。 The server was not found or was not accessible. 服务器未找到或无法访问。 Verify that the instance name is correct and that SQL Server is configured to allow remote connections. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details. (提供者:SQL网络接口,错误:50-发生本地数据库运行时错误。无法创建自动实例。有关错误详细信息,请参阅Windows应用程序事件日志。

DbInitiliazer.cs DbInitiliazer.cs

 using NMBSAPICore.Models;
 using System;
 using System.Linq;
 namespace NMBSAPICore.Data
 {
    public class DbInitializer
    {
        public static void Initialize(NMBSAPICoreContext context)
        {
            if (context.Station.Any())
            {
                return;   // DB has been seeded
            }
            var stations = new Station[] {
                    new Station { Naam = "Olen", Sporen = 2 }
            };
            foreach (Station s in stations)
            {
                context.Station.Add(s);
            }
            context.SaveChanges();
         }
    }
}

Startup.cs configure Startup.cs配置

public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<NMBSAPICoreContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("NMBSAPICoreContext")));

        services.AddMvc();

        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info { Title = " NMBS API", Version = "v1" });
        });


    }public void Configure(IApplicationBuilder app, IHostingEnvironment env , NMBSAPICoreContext NMBSAPIContext)
    {
        app.UseSwagger();
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
        });
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseAuthentication();
        app.UseMvc();
        DbInitializer.Initialize(NMBSAPIContext);
    }
}

Context file 上下文文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Protocols;

namespace NMBSAPICore.Models
{
public class NMBSAPICoreContext : DbContext
{
public NMBSAPICoreContext(DbContextOptions<NMBSAPICoreContext>options):
base(options)
    {
    }

    public DbSet<Station> Station { get; set; }


    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Station>().ToTable("Stations");
    }
  }
}

Connectionstring: 连接字符串:

"ConnectionStrings": {
"NMBSAPICoreContext": "Server=(localdb)\\mssqllocaldb;Database=NMBSDb;Trusted_Connection=True;MultipleActiveResultSets=true"
  }

sqllocaldb info sqllocaldb信息

.\IIS_DB
aspnetIceOnWheels
aspnet-IceOnWheels-1
localdb
mssqllocald
MSSQLLocalDB
NMBSAPICore
ProjectsV13
v11.0

I didn't want to post too much code because it isn't relevant to my issue because it also happen to fresh new project and make and try to run with a localDb. 我不想发布过多的代码,因为它与我的问题无关,因为它也发生在新的新项目上并制作并尝试使用localDb运行。

As you can see I use the Swagger api so I can't easily test my API calls. 如您所见,我使用Swagger API,因此无法轻松测试API调用。 I have multiple computers and on none of them did the localdb generating work. 我有多台计算机,但在其中一台计算机上都没有localdb生成工作。 Does that mean I have installed SQL Server Express on all devices wrong? 这是否意味着我在所有设备上都安装了SQL Server Express? Microsoft states that I should generate by default a mdf file in /users/name/ , mine does nothing. Microsoft声明我默认情况下应该在/users/name/生成一个mdf文件,而我的则不执行任何操作。

I have installed SMSS and SQL Server Express 2017. 我已经安装了SMSS和SQL Server Express 2017。

I will answer this myself , turn out i used a wrong connection string then there were more errors about permission to my personal folder. 我将自己回答这个问题,结果证明我使用了错误的连接字符串,然后关于我的个人文件夹的许可出现了更多错误。

After that i got and error about db currency update => Microsoft documentation helped me very well with this issue 之后,我得到了关于db货币更新的错误=> Microsoft文档很好地帮助了我解决这个问题

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

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