Book Image

Entity Framework Tutorial

By : Joydip Kanjilal
Book Image

Entity Framework Tutorial

By: Joydip Kanjilal

Overview of this book

<p>The ADO.NET Entity Framework is a new way to build the data access layer of your Windows or web applications. It's an Object Relational Mapping (ORM) technology that makes it easy to tie together the data in your database with the objects in your applications, by abstracting the object model of an application from its relational or logical model.<br /><br />This clear and concise book gets you started with the Entity Framework and carefully gives you the skills to speed up your application development by constructing a better data access layer. It shows you how to get the most from the ADO.NET Entity Framework to perform CRUD operations with complex data in your applications.<br /><br />This tutorial starts out with the basics of the Entity Framework, showing plenty of examples to get you started using it in your own code. You will learn how to create an Entity Data Model, and then take this further with Entity types. You will also learn about the Entity Client data provider, learn how to create statements in Entity SQL, and get to grips with ADO.NET Data Services, also known as Project Astoria.</p>
Table of Contents (13 chapters)
Entity Framework Tutorial
Credits
About the Author
About the Reviewer
Preface

Adding, Modifying, and Deleting Objects


In this section, we will discuss how you can use ObjectServices to add, modify, and delete an object within an ObjectContext.

To add an object to the ObjectContext requires the use of the AddObject method. It accepts the entity set name and entity instance as parameters and adds the object passed to it to the ObjectContext. Once you have added an object to the ObjectContext, you can call the SaveChanges to persist the changes to the database.

Here is an example that shows how you can add an object to the ObjectContext and then call the SaveChanges method to persist the changes to the database:

PayrollModel.PayrollEntities ctx = new PayrollModel.PayrollEntities();
PayrollModel.Employee employee = new PayrollModel.Employee();
employee.EmployeeID = 16;
employee.EmployeeName = "Debanjan Banerjee";
employee.EmployeeAddress = "Kolkata";
employee.JoiningDate = DateTime.Now;
employee.Department = ctx.Department.
Where(d => d.DepartmentID == 3).First();
ctx...