Friday, June 7, 2024

Add ConnectionString .Net core 8

 

appsettings.json

 Step 1:Add Connection String to appsettings.json

"ConnectionStrings": {
  "DefaultConnection": "Server=ALLEN\\SQLEXPRESS;Database=Bulky;User Id=sa;Password=123;Trusted_Connection=True;TrustServerCertificate=True;"

}

 

 Step 2:Add a class called ApplicationDbContext


 using Microsoft.EntityFrameworkCore;

namespace BulkyWeb.Data
{
    public class ApplicationDbContext:DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options):base(options)
        {
            
        }
    }
}

Step 3:Configure Connection string in Program.cs


 

 var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<ApplicationDbContext>(options=>options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));