Consider we have a new project 'countries’,
for adding country names to database. For this first we want to create
DbContext class and a connection string to connect to the database.
Step 1: Create Model classes
Suppose our database name is 'CountryDB' having one table 'Country''
Add a Model class and give name it as 'Country.cs'
Country.cs
Step 1: Create Model classes
Suppose our database name is 'CountryDB' having one table 'Country''
Add a Model class and give name it as 'Country.cs'
Country.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.ComponentModel.DataAnnotations;
namespace
countries.Models
{
public class Country
{
[Key]
public virtual int country_id {get;
set; }
[Required ]
public virtual string country_name {
get; set;}
}
}
Add DbContext class.Right click on Models>>Add>>Class name it as 'CountryDB'
CountryDB.cs
Add DbContext class.Right click on Models>>Add>>Class name it as 'CountryDB'
CountryDB.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Data.Entity;
namespace
countries.Models
{
public class CountryDB:DbContext
{
public DbSet<country>Country {
get; set; }
}
}
Step 2: Change the connection string
Connection String name must be same as the database name,ie 'CountryDB'
Change the connection string in Web.config file.
Connection String name must be same as the database name,ie 'CountryDB'
Change the connection string in Web.config file.
<connectionStrings>
<add name="CountryDB" connectionString="Data
Source=LEEDHAR1-PC\SQLEXPRESS; Database=CountryDB;uid=asd;pwd=asd;Trusted_Connection=False;Persist
Security Info=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
No comments:
Post a Comment