Entity Framework Core 3.1 學習筆記10 在 ASP.NET Core 中配置 EF Core

democontext:

  public DemoContext(DbContextOptions<DemoContext> options):base(options) {
           
        }

nuget包:

    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.4" />

Startup

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddDbContext<DemoContext>(options => {
                options.UseSqlServer(Configuration.GetConnectionString("LocalDb"));
            });
        }

appsettings.json添加連接字符串:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "ConnectionStrings": {
    "LocalDb": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Demo;"
  },
  "AllowedHosts": "*"
}

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章