Monday 27 April 2015

Entity framework in asp.net


Microsoft make our life easier to provide the Entity frame work and release us to use the traditional way to use the database connectivity. It provides an O/RM framework called "Entity Framework" to automate database.

By Entity Framework in Asp.net, developer’s can queries using LINQ, then retrieve, Insert, Update and manipulate data as strongly typed objects. 

Entity Framework can be useful on any type of architecture opted by you either is code first approach or data driven approach.

 In Code first approach either you can have all the code views or by this you want to create the data base.

In Data Driven approach you have database ready and by which you want to create the classes that will interact with the database.

I am going to demonstrate the Data driven approach. It’s really good for beginners for starting the entity framework.

11 Steps to create Entity Framework and work with it.


1.       Suppose you have data base with below given table.




2.       Open your visual Studio and select web application. You can see the below files in the solution explorer.


3.       Creating the Entity framework. Right click on project Expolrer à Add à New Item



4.       Go to the data and select the “data” from left and then select Entity Data Model  for creating the Entity framework classes and provide the name.



5.       Now the wizard will start and it will display like this. You have to click on Next over there.



6.       Now in Next screen create the connection string for entity framework.


7.       Creating the connection string from below window. Select the Server and the Datasource name as the provider.  Like Below Screen. [Click on test connection to check the it is connected to the database properly or not]



8.       Once you Click on Ok to the connection window you will get the window like below with all the details. Now afterword click on Next.


9.       It will ask for list of table that you can select. For making the classes and to connect to database.
Select all the tables and click on finish;


10.   Once you click on finish you can see the list of tables and classes have created in your application which will help you to interact with database from LinQ query through the Entity Framework.


11.   Now you can use that classes to insert, Update, retrieve and Delete the records from the data base. By using like below;
using (LibraryManagementSystemEntities obj = new LibraryManagementSystemEntities())
            {
                return (from oCategory in obj.BookCategories
                        select new BookCategorys
                        {
                            BookCategoryId = oCategory.BookCategoryId ,
                            BookCategory  = oCategory.BookCategoryName
                        }).ToList();
          }




To know the more details about how to insert, Update and Delete into entity frame work please
 refer to the next Blog as given below;

No comments:

Post a Comment