Disclaimer:I am trying to learn more about net 6.0 and am a rookie.
Here is what i am trying to do:
Here is what I did:
Most of this Context file is automatically generated and the only thing i did was updated the public TransparencyContext(IConfiguration config) to include the Iconfig so i could access it in optionsBuilder.UseSqlServer(config.GetConnectionString("SQLDB"));
TransparencyContext.cs
namespace EFTutorial.Models
{
public partial class TransparencyContext : DbContext
{
private readonly IConfiguration config;
public TransparencyContext()
{
}
public TransparencyContext(IConfiguration config)
{
this.config = config;
}
public TransparencyContext(DbContextOptions<TransparencyContext> options)
: base(options)
{
}
public virtual DbSet<Fundraiser> Fundraisers { get; set; } = null!;
public virtual DbSet<Person> Persons { get; set; } = null!;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(config.GetConnectionString("SQLDB"));
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Fundraiser>(entity =>
{
entity.Property(e => e.Description).IsUnicode(false);
entity.Property(e => e.EndDate).HasColumnType("datetime");
entity.Property(e => e.Goal).HasColumnType("decimal(18, 2)");
entity.Property(e => e.Name)
.HasMaxLength(1000)
.IsUnicode(false);
});
modelBuilder.Entity<Person>(entity =>
{
entity.Property(e => e.DateOfBirth).HasColumnType("datetime");
entity.Property(e => e.FirstName)
.HasMaxLength(100)
.IsUnicode(false);
entity.Property(e => e.LastName)
.HasMaxLength(100)
.IsUnicode(false);
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
}
I try then to test it from home controller by doing this.
private TransparencyContext _transparencyContext;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
_transparencyContext = new();
}
public IActionResult Index()
{
var p = new Person();
p.FirstName = "Entity";
p.LastName = "Framework";
_transparencyContext.Persons.Add(p);
_transparencyContext.SaveChanges();
return View();
}
When i do this i get the config variable( in TransparencyContext) being null (System.ArgumentNullException: 'Value cannot be null(Parameter 'connectionString')'). I havent changed my program.cs and is the way it was when the project was created.
Program.cs
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
I know that configuration file can be accessed from app.Configuration but not sure how to make configuration accessible for the TransparencyContext.cs so i can get the connection string for db.I have tried looking at Microsoft documents but they don't show how they make the Iconfiguration avaiable and only show them using it.Any help is much appreciated.
I am thinking I might need to register a service to configuration but not sure how to do it.
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"SQLDB": "Server=AzureConnectionStringCopyPaste"
}
}
The quick fix to your current setup would be just injecting IConfiguration into the controller and using it to construct the context:
public HomeController(ILogger<HomeController> logger, IConfiguration cfg)
{
_logger = logger;
_transparencyContext = new(cfg);
}
But "correct" and idiomatic way is to use DI to register and resolve the context:
TransparencyContext(DbContextOptions<TransparencyContext> options)AddDbContext or AddDbContextFactory:builder.Services.AddDbContextFactory<TransparencyContext>(opts =>
opts.UseSqlServer(builder.Configuration.GetConnectionString("SQLDB")));
public HomeController(ILogger<HomeController> logger, TransparencyContext ctx)
{
_logger = logger;
_transparencyContext = ctx;
}
the default configuration file name is appsettings.json (not AppSettings.Json)
You should configure your configuration in the host builder. Example:
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", false, true)
.AddEnvironmentVariables()
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", true, true)
.Build();
builder.UseConfiguration(configuration)
Also you can use WebBuilder method .ConfigureAppConfiguration to configure is using AppConfiguration
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With