Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted Jul 24, 2024, 6:53 AM EDT
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:
public class ApplicationDbContext : DbContext { public ApplicationDbContext(DbContextOptions options) : base(options) { } // DbSets for your entities }public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddDbContext(options => options.UseSqlServer("name=ConnectionStrings:DefaultConnection")); }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 SatasiyaRank #302 · 8.3K reputationPosted Jul 24, 2024, 12:55 AM EDT
Hi Will, can you provide some details for your question? what exectly you looking for?
Abhishek YadavRank #217 · 11.3K reputationPosted Jul 28, 2024, 1:54 AM EDT
using System; using System.Data.SqlClient; class ConnectedModeExample { static void Main() { string connectionString = "your_connection_string_here"; string query = "SELECT * FROM YourTable"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = new SqlCommand(query, connection); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { Console.WriteLine($"{reader["ColumnName"]}"); } } } } }Vishal YelveRank #178 · 13.6K reputationPosted Jul 29, 2024, 2:18 AM EDT
Hi Will,
create APIDbContext Class which will inherit from DbContext class.
public class APIDbContext: DbContext { public APIDbContext(DbContextOptions options): base(options) {} public DbSet Departments { get; set; } public DbSet Employees { get; set; } }add constructor in your repository
private readonly APIDbContext _appDBContext; public DepartmentRepository(APIDbContext context) { _appDBContext = context ?? throw new ArgumentNullException(nameof(context)); }Don't forget to register
services.AddScoped (); services.AddScoped (); services.AddDbContext (options => options.UseSqlServer(Configuration.GetConnectionString("EmployeeAppCon")));do refer to my article for a complete understanding
https://www.c-sharpcorner.com/article/net-core-3-1-web-api-crud/