简体   繁体   中英

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. 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

System.Data.SqlClient.SqlException: 'A network-related or instance-specific error occurred while establishing a connection to 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. (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.

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

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

.\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.

As you can see I use the Swagger api so I can't easily test my API calls. I have multiple computers and on none of them did the localdb generating work. Does that mean I have installed SQL Server Express on all devices wrong? Microsoft states that I should generate by default a mdf file in /users/name/ , mine does nothing.

I have installed SMSS and 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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