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

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();
        }

 

posted @   守护月亮的星星  阅读(225)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
点击右上角即可分享
微信分享提示