Saturday, 5 November 2016

DataBase First Approach With Asp.Net MVC Framework

What is Entity Frame Work?

  • Entity Framework is an ORM Framework.
  • ORM stands for Object Relational Mapping.
  • ORM automatically creates the classes based on database tables and vice versa.
  • By using ORM framework we can save a lot of time and effort when interact with database.
  • we have three approaches.
  1. Database approach
  2. Model -First approach
  3. Code -First approach
  • we can choose any one of the above approach to reduce the time in development.

Understanding the Database Approach


First create the Database with the Name of DatabaseFirstApproach in SQL SERVER.
Now create the table withe name Departments and Employees.

/* create Database */
create database DatabaseFirstApproach 


/* Use Databse */
use DatabaseFirstApproach 


/* create Departments */
create table Departments
(
Id int primary key not null,
Names varchar(40),
Locations varchar(40)
)

/* create Employee*/

create table Employees
(
Id int primary key identity(1,1),
FirstNames varchar(40),
LastName varchar(40),
Gender varchar(40),
Salary int,
DepartmentId int foreign key references Departments(Id)
)

/* Insert Values in Departments */

insert into Departments values(1,'IT','NewYork')
insert into Departments values(2,'HR','London')
insert into Departments values(3,'Payroll','Sidney')

/* Insert Values in Employee */

insert into Employees values('Mark','Hasting','Male',6000,1)
insert into Employees values('Murali','Krishna','Male',9000,2)
insert into Employees values('Mark','Hasting','Male',2000,1)
insert into Employees values('Mark','Hasting','Male',1000,2)
insert into Employees values('Mark','Hasting','Male',8000,3)
insert into Employees values('Mark','Hasting','Male',9000,2)

/* Select Department and Employee for check the values are inserted or not */
select *from Employee
select *from Departments


  • Next Open Visual Studio of any version and create a new project
  • goto File --> select Website -> choose website empty template
  • now open Solution Explorer -->Right click on solution check Manage NuGet Packages for Solution.
  • if it is not available then go to tools -->check Extension and Updates 
  • one pop up Wizard appear and see a search box at right side of  wizard.
  • now you check NuGet Packages manager and install it.
  • now goto solution explorer -->right click -->check Solution check Manage NuGet Packages for Solution 
  • Now open it search for Entity Framework and install it.
  • Now right click on DatabaseFirstApproach Project -->add-->Asp.net Folder-->add App_Code.
  • Now right click on App_Code -->add -->AddNewItem --> and search for ADO.NET ENTITY DATA MODEL.

DataBase First Approach With Asp.Net MVC Framework

DataBase First Approach With Asp.Net MVC Framework
DataBase First Approach With Asp.Net MVC Framework
  • Now add new form to the project and drag and drop the GridView and EntityDataSource

          <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
        <asp:EntityDataSource ID="EntityDataSource1" runat="server">
        </asp:EntityDataSource>


  • Next Flip into Design and go to Entity Data Source Task --> Configure it . Before Configure .You should Build the Project.
  • next go to grid View  Task and choose the Entity Data Source. and build it finally run the program. you can see the out put






No comments:

Post a Comment