C
C#17mo ago
TotallyJustin

❔ Entity Framework 7 - Where to call EnsureCreared method

I'm trying to create a PGSQL database with a code-first approach using EF7 in my ASP.NET Core Web API application. I have created the models, the ApplicationDbContext and the connection string, and want to know how to call the EnsureCreated method rather than using a migration to an existing database. Previously in .NET 6, it was called in the ConfigurationServices method in the Startup.cs file, but I'm struggling to find how top do the equivalent in Program.cs, which Startup.cs has been integrated into as part of .NET 7. Thanks in advance!
22 Replies
Angius
Angius17mo ago
If it was called on services, call it on application.Services, IIRC
TotallyJustin
TotallyJustin17mo ago
This is a screenshot of how it was done in .NET 6
TotallyJustin
TotallyJustin17mo ago
This is it in .NET 7, I'm not sure where the method should be called
Angius
Angius17mo ago
Just add it somewhere after builder.Services.AddDbContext() then
TotallyJustin
TotallyJustin17mo ago
Sorry if I'm being slow but do I need to put something before application.Services.[...]?
Angius
Angius17mo ago
Angius
Angius17mo ago
This code
TotallyJustin
TotallyJustin17mo ago
Sorry again I don't quite follow, I assume it's not meant to be like this?
Angius
Angius17mo ago
Remove that app.Services....? line See what the error is with ApplicationDbContext
TotallyJustin
TotallyJustin17mo ago
CS7036: There is no argument given that corresponds to the required parameter 'opts' of 'ApplicationDbContext.ApplicationDbContext(DbContextOptions<ApplicationDbContext>)'
TotallyJustin
TotallyJustin17mo ago
ApplicationDbContext.cs
Angius
Angius17mo ago
Ah
Angius
Angius17mo ago
Stack Overflow
How do you do database.ensurecreated() in aspnet core web applicati...
In a .NET 5 web application, we use code such as the following in startup.cs to initialize the DB using Entity Framework: using (var serviceScope = app.ApplicationServices.GetService<
Angius
Angius17mo ago
Try this to get the dbcontext
TotallyJustin
TotallyJustin17mo ago
Already looked at that, it has the same issue of being for .NET 6. I’ve used EF 7 before just not by using the EnsureCreated method and instead using migrations
Monsieur Wholesome
That code works for both .net 6 and .net 7 tho
TotallyJustin
TotallyJustin17mo ago
I’ll give it another try
TotallyJustin
TotallyJustin17mo ago
I tried adding this code and I'm not sure how to resolve the issue
Monsieur Wholesome
dbcontexts need to be instantiated within a scope .
TotallyJustin
TotallyJustin17mo ago
You are a saint Thank you so much
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ C++ HelpThe code is: ```cpp vector<string> s_split(string Str,string Seperator) { vector<string> SplitSt❔ Need help with this part of a programming assignmenthello i am brand new to c# programming and i am stuck on this part of psuedocode and was wondering i✅ Make process believe it's a startup app?Hello! So I'm building an app that starts another app. The app in question seems to behave differen❔ Board Game (Marvel Champions) Gameplay Simulator - deep clone is very inefficientI have coded a program that simulates X games of Marvel Champions (a living card game by FFG). It wo❔ Problem of collections blocking when they shouldn't (BlockingCollection/ConcurrentQueue)Hello there, I have a problem with the producer-consumer patron in an application in net 6 (console❔ How do i run a c# programHello I wrote a new program in .txt file, how do i run this? It goes: Console WriteLine ()"Hello Wor✅ WPF SliderHello, I've bind my array of double to my slider but i do not know how to use the slider in order toCross Thread operation not valid: control 'CardStatusField' acessed from a thread other than the thr```c namespace Project1 { public partial class Form1 : Form { NFCReader NFC = new NF❔ ✅ Splitting string without allocationI'm writing a JsonConverter for Vector3 and I'd like to do this without allocating too much memory. ❔ How to correctly use Roslyn to parse RegionDirectiveTriviaSyntax in a project?I would like to ask a question about parsing syntax tree for #region preprocessor directive. For exa