C
C#4w ago
tama

How does EF know what database to use?

Hello there. I am building an entity related management system, and I'm wondering how EF know what database I want it to use. I am using .Net 8, and the only place I have referenced the database was in the Program.cs file, however, I have commented out that part and it is still able to update my database. How is that possible?
7 Replies
Pobiega
Pobiega4w ago
EF uses the connectionstring you have given, either in the context itself or via the context options object, often configured via Program.cs if you show your code, Im sure we can find where its reading the connstr from
tama
tama4w ago
My DBContext:
C#
public class CarContext : DbContext
{
public CarContext(DbContextOptions<CarContext> options) : base(options)
{
}

public DbSet<Car> Cars { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Car>()
.Property(d => d.CarType)
.HasConversion<string>();

modelBuilder.Entity<Car>()
.Property(d => d.Status)
.HasConversion<string>();
}
}
C#
public class CarContext : DbContext
{
public CarContext(DbContextOptions<CarContext> options) : base(options)
{
}

public DbSet<Car> Cars { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Car>()
.Property(d => d.CarType)
.HasConversion<string>();

modelBuilder.Entity<Car>()
.Property(d => d.Status)
.HasConversion<string>();
}
}
Program.cs:
C#
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using CarManagementSystem.Infrastructure.Data;
using CarManagementSystem.Application.Services;
using CarManagementSystem.Core.Interfaces;
using CarManagementSystem.Infrastructure.Repositories;
using CarManagementSystem.Core.Entities;
using CarManagementSystem.Helpers;

Console.WriteLine(Guid.NewGuid().ToString());


/*var host = Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) =>
{
// Register DbContext
services.AddDbContext<CarContext>(options =>
options.UseSqlServer("REAL CONNECTION STRING HOWEVER OUT-COMMENTED"));

// Register repositories and services
services.AddScoped<ICarRepository, CarRepository>();
services.AddScoped<CarService>();
})
.Build();

// Rest of your code remains unchanged
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;

try
{
var CarService = services.GetRequiredService<CarService>();

// Example usage of the CarService
var Car = new Car
{
SerialNumber = Guid.NewGuid(),
ModelId = "",
ModelName = "",
Manufacturer = "",
PrimaryUser = "",
CarType = CarType.SUV,
Status = CarStatus.Active
};

CarService.AddCar(Car);

var activeCars = CarService.GetActiveCars();
foreach (var d in activeCars)
{
Console.WriteLine($"{d.ModelName} - {d.Status}");
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}

await host.RunAsync();*/
C#
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using CarManagementSystem.Infrastructure.Data;
using CarManagementSystem.Application.Services;
using CarManagementSystem.Core.Interfaces;
using CarManagementSystem.Infrastructure.Repositories;
using CarManagementSystem.Core.Entities;
using CarManagementSystem.Helpers;

Console.WriteLine(Guid.NewGuid().ToString());


/*var host = Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) =>
{
// Register DbContext
services.AddDbContext<CarContext>(options =>
options.UseSqlServer("REAL CONNECTION STRING HOWEVER OUT-COMMENTED"));

// Register repositories and services
services.AddScoped<ICarRepository, CarRepository>();
services.AddScoped<CarService>();
})
.Build();

// Rest of your code remains unchanged
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;

try
{
var CarService = services.GetRequiredService<CarService>();

// Example usage of the CarService
var Car = new Car
{
SerialNumber = Guid.NewGuid(),
ModelId = "",
ModelName = "",
Manufacturer = "",
PrimaryUser = "",
CarType = CarType.SUV,
Status = CarStatus.Active
};

CarService.AddCar(Car);

var activeCars = CarService.GetActiveCars();
foreach (var d in activeCars)
{
Console.WriteLine($"{d.ModelName} - {d.Status}");
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}

await host.RunAsync();*/
at some point, the program.cs code was not out-commented, so i wonder if it saved a copy of the connection string somewhere
Pobiega
Pobiega4w ago
uh if you comment out all of program.cs, your program wont compile and run so its using the old built version thats my guess at least ie, its still using the old version that had all of program.cs
tama
tama4w ago
its built like a console app, however, it's used as a library for another project in my solution if that makes sense
Pobiega
Pobiega4w ago
dont do that build it as a library if its a library and then you'd let the consuming app worry about the settings/connstr
tama
tama4w ago
im building a function app for azure, so would it make sense to move all my library stuff to the function app project?
Pobiega
Pobiega4w ago
¯\_(ツ)_/¯ Dunno Have never built azure functions for anything other than small snippets