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": "*"
}

  

posted @ 2021-04-11 23:01  tofight  阅读(14)  评论(0编辑  收藏  举报