C
C#2w ago
surwren

Entity Framework Core: How to build without Package Manager Commands?

I am looking at this sample project : https://github.com/Nedal-Esrar/Travel-and-Accommodation-Booking-Platform I notice that it automatically creates the TABP table as long as the connection string goes through, which is not the same for other EFCore projects I have created in the past in .NET 8.0. I have always needed to manually do Add-Migration "Add_Tables" and Update-Database but it seems this project does not? I did ctrl + shift + f but I do not see any scripts for the aforementioned commands. What dark magic is this project doing that bypasses the migrations step?
GitHub
GitHub - Nedal-Esrar/Travel-and-Accommodation-Booking-Platform: Hot...
Hotel Booking API implemented with ASP.NET Core and built upon the principles of clean architecture. - Nedal-Esrar/Travel-and-Accommodation-Booking-Platform
4 Replies
Kringe
Kringe2w ago
They are probably using EnsureCreated() more info https://learn.microsoft.com/en-us/ef/core/managing-schemas/ensure-created
Create and Drop APIs - EF Core
APIs for creating and dropping databases with Entity Framework Core
glhays
glhays2w ago
I think you can also add the database. Migrate in the on model creating override. Another way is through DI registration.
public StorageBroker(IConfiguration configuration)
{
this.configuration = configuration;
this.Database.Migrate();
}
public StorageBroker(IConfiguration configuration)
{
this.configuration = configuration;
this.Database.Migrate();
}
Here the StorageBtroker is registered in DI with the command in its constructor. See line 78 of: src/TABP.Infrastructure/Persistence/PersistenceConfiguration.cs in the repo you posted. ^^^^^^^^
surwren
surwrenOP7d ago
Yes, thank you! I didn't know that calling a method automates the migration process. Are there drawbacks to this?
glhays
glhays7d ago
Only if for some reason you need to remove a migration before the next one occurs automatically.
Want results from more Discord servers?
Add your server