adding a context file
Loading
adding a context file
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Vishal YelvePosted Jul 29, 2024, 6:18 AM
Hi Will,
create APIDbContext Class which will inherit from DbContext class.
add constructor in your repository
Don't forget to register
do refer to my article for a complete understanding
https://www.c-sharpcorner.com/article/net-core-3-1-web-api-crud/
Abhishek YadavPosted Jul 28, 2024, 5:54 AM
Naimish MakwanaPosted Jul 24, 2024, 10:53 AM
Sure, I can guide you on how to add a context file in .NET Core. Here are the steps:
Create a new C# class AppDbContext: This will be your context class and you can choose any name you want1.
Inherit from the base class DbContext: Your newly created class should inherit from the DbContext class1.
Inject DbContextOptions into constructor and pass it to the base class : This is done to configure the DbContext1.
Here’s a basic example of what your DbContext class could look like:
ApplicationDbContextis the DbContext subclass and it’s registered as a scoped service in the ASP.NET Core application service provider2. The context is configured to use the SQL Server database provider and will read the connection string from ASP.NET Core configuration2.Remember, it’s very important to dispose the DbContext after use. This ensures both that any unmanaged resources are freed, and that any events or other hooks are unregistered so as to prevent memory leaks in case the instance remains referenced2.
Thanks
Nikunj SatasiyaPosted Jul 24, 2024, 4:55 AM
Hi Will, can you provide some details for your question? what exectly you looking for?