0% found this document useful (0 votes)
8 views

AWD-Lecture 03 DataBase Connection

ASPDOTNETCONNECTION

Uploaded by

hayatsafi077
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

AWD-Lecture 03 DataBase Connection

ASPDOTNETCONNECTION

Uploaded by

hayatsafi077
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Asp.

Net Core
Database connection

Nangarhar University Computer Science Faculty –(NUCSF) 1401/11/17


Outlines
1. Connection with Sql Server
1. DbContext
2. appSettings.json file
3. Add to service contianer
Dbcontext configuration
➢DbContext class:
➢First we need to create a dbContext class of our application with anyname
and must be inherited form the Base DbContext class.
➢To configure the dbcontext class in Dependecy injection we need a nuget
package called: Microsoft.EntityFrameworkCore;

• public class StoreMISDBConnection :DbContext {


• public StoreMISDBConnection(DbContextOptions<StoreMISDBConnection>
options)
• :base(options)
• {}
public DbSet<Product> Products { get; set; }
• }
Dbcontext configuration

For whole connection process three process are needed:


1. Microsoft.EntityFrameworkCore;
2. Microsoft.EntityFrameworkCore.SqlServer;
3. Microsoft.EntityFrameworkCore.Tools;
Confugiring Appsettings.js file for connection

➢Add ConnctionString in AppSetting.json file:


➢After creating the dbContext class the second step is to make a
connction string for database in appSetting.json file.

• "ConnectionStrings": {
• "StoreMISConnectionString":"server=ZIAPC;database=StoreDB;trusted_connection=true"
• }
Configuring Program.cs
Portion of Code for configuration sql server in program.cs:

• builder.Services.AddDbContext<StoreMISDBConnection>(options => {

options.UseSqlServer(builder.Configuration.GetConnectionString("StoreMISCon
nectionString"));
• });
Its your turn to ask about today’s lecture

You might also like