首页 > 数据库 >.NET 6配置EF Core数据库链接字符串

.NET 6配置EF Core数据库链接字符串

时间:2022-10-10 11:24:59浏览次数:59  
标签:Core ConnectionString app EF 字符串 var NET builder

appsetting.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "ConnectionString": "server=localhost;Database=FakeXieChengDb;User Id=sa;Password=123"
  }
}

 配置Program文件

public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);
            //获取配置字符串
            var connectionString = builder.Configuration.GetConnectionString("ConnectionString");
            builder.Services.AddControllers();
            //添加上下文呢服务
            builder.Services.AddDbContext<APPDbContext>(option =>
            {
                option.UseSqlServer(connectionString);
            });


            var app = builder.Build();
            if (app.Environment.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            
            app.MapControllers();

            app.Run();
        }

 

标签:Core,ConnectionString,app,EF,字符串,var,NET,builder
From: https://www.cnblogs.com/budongdong/p/16774972.html

相关文章