In this post I will walkthrough you on performing basic CRUD operation using ADO.Net Entity framework.
Database design
I am going to use School Database. You can find School database script here . To be precise, I am going perform CRUD operation on only one table Person. Schema of Person table is as below.
I am going to perform CRUD Operation on Person table from a console application.
Create Data Model
- Right click on Console Application and add new Item
- From Data Tab select ADO.Net Entity Data Model .If you want you can change name of the model.
3. Click on Add button; you will get dialog box. From there select Generate from database option
4. Create a new connection
5. Give database server name and choose database from the drop down as given below,
After clicking on Ok button, you will notice that you come to previous dialog box and a connection setting got created. Name of the connection setting is as below,
Database name + Entities = default connection setting name
If database is School then default connection setting name would be schoolentites. However, if you want you can change default connection setting name as desired.
6. Now you need to choose Data objects to make them part of Data Model. From below dialog box you can choose tables, stored procedures and views.
Since we are going to perform CRUD operation only on Person table , so I will choose only Person table to be part of Data Model.
If you want you can change Model Namespace name. By default it would be Database name suffixed with Model.
Now click Finish to create data model.
Performing CRUD operation
Retrieving all the records
You can fetch all the records as below.
Add a person
You can add a person in three steps
- Create object of Person
- Call add object on pluralize Person entity. In this case this is People
- Call save changes on entities
Modify a Person
To modify a person you need to perform three steps
- Fetch the Person to be modified
- Modify the desired values of the columns
- Call save changes on the entities
Delete a Person
To modify a person you need to perform three steps
- Fetch the Person to be deleted
- Call delete object on entities
- Call save changes on the entities
Full source code is given below,
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ADONetEntityModelfirstDemo { class Program { static void Main(string[] args) { #region Reterive a Person SchoolEntities entities = new SchoolEntities(); var result = from r in entities.People select r; foreach (var r in result) { Console.WriteLine(r.PersonID + r.LastName); } Console.ReadKey(true); #endregion #region Add a Pesron entities.People.AddObject(new Person { FirstName = "dhananjay", LastName = "kumar" }); entities.SaveChanges(); #endregion #region Modify a Person Person personToModofy = (from r in entities.People.Where (a => a.PersonID == 1) select r).FirstOrDefault(); personToModofy.LastName = "Jamshedpur"; entities.SaveChanges(); #endregion #region Delete a Person Person personToDelete = (from r in entities.People.Where (a => a.PersonID == 1) select r).FirstOrDefault(); entities.People.DeleteObject(personToDelete); entities.SaveChanges(); #endregion #region Reterive a Person var result1 = from r in entities.People select r; foreach (var r in result1) { Console.WriteLine(r.PersonID + r.LastName); } Console.ReadKey(true); } #endregion } }
In further post, we will discuss other aspects of ADO.Net Entity framework. I hope this post was useful. Thanks for reading
Follow @debugmode_
Leave a Reply