How can I implement DbContext Connection String in .NET Core?

后端 未结 5 1009
生来不讨喜
生来不讨喜 2020-12-25 12:14

My situation is quite similar to this link or at least my code is similar and I am trying to find a way to apply this same method in .NET Core syntax.

Pass connectio

5条回答
  •  太阳男子
    2020-12-25 13:00

    IMO best practice:

    add to your configuration.json:

         "ConnectionStrings": {
        "BooksContext": "Server=MyServer;Database=MyDb;Trusted_Connection=True;"
      }
    

    and to initialize section:

    services.AddDbContext(options => options.UseSqlServer(configuration.GetConnectionString(nameof(BooksContext))));
    

提交回复
热议问题