Advertisement
otkalce

Entity Framework - configuration

Apr 5th, 2023 (edited)
694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | Source Code | 0 0
  1. # *** Replace tokens in {curly braces} and run the command to reverse engineer database
  2.  
  3. C:\Users\{your-username-here}\.dotnet\tools\dotnet-ef.exe dbcontext scaffold "server={your-server-here};Database=Task07;User={your-username-here};Password={your-password-here};TrustServerCertificate=True;MultipleActiveResultSets=true" Microsoft.EntityFrameworkCore.SqlServer -o Models
  4.  
  5. # *** Open models in Visual Studio, find and open db context and find OnConfiguring method
  6.  
  7. // *** Add the following section to appsettings.json - replace tokens in {curly brace} with the connection string found in OnConfiguring method ***
  8. "ConnectionStrings": {
  9. "AudioConnStr": "{copy-from-onconfiguring-method}"
  10. }
  11.  
  12. // *** In OnConfiguring(), replace hardcoded conn. str. with the configuration parameter. Also, remove the big #warning, we don't need it any more.
  13. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  14. => optionsBuilder.UseSqlServer("Name=ConnectionStrings:AudioConnStr");
  15.  
  16. // *** Register the db context in the DI container ***
  17. builder.Services.AddDbContext<Task07Context>(options =>
  18. {
  19. options.UseSqlServer("name=ConnectionStrings:AudioConnStr");
  20. });
  21.  
  22.  
Tags: ef-setup
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement