danila_net
@danila_net
Изучаю CLR, C#, ASP.NET

Почему приложение .net core не видит строку подключения?

Приложение на .net core не видит строку подключения ни в program.cd ни в appsettings.json. Подключается к БД не тем пользователем, который указан в строке подключения
Program.cs:
using HealthCore.Data;
using HealthCore.Models;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddControllersWithViews();

builder.Services.AddDbContext<HealthCoreContext>(options =>
   options.UseSqlServer("Data Source=s25hv;Initial Catalog=HealthCore;Integrated Security=True;User ID=relax55;Password=Cd3p"));

//builder.Services.AddDbContext<HealthCoreContext>(options =>
//   options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

// Add services to the container.
builder.Services.AddControllersWithViews();

builder.Services.AddAuthentication(
    CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(option => {
        option.LoginPath = "/Access/Login";
        option.ExpireTimeSpan = TimeSpan.FromMinutes(20);

    });


var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseMigrationsEndPoint();
}
else
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages();

app.Run();
  • Вопрос задан
  • 136 просмотров
Пригласить эксперта
Ответы на вопрос 1
1) Подключись через Server Explorer в Visual Studio к базе данных
2) Нажав на созданное подключение, можно в Properties увидеть строку подключения.

Вот наглядно:
https://www.youtube.com/watch?v=5xyHrqWQXmc
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы