overview of entity framework by software outsourcing company india

20
Prepared By: Mr. Dhrumil Patel iFour Consultancy Entity Framework

Upload: jignesh-aakoliya

Post on 20-Jan-2017

33 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Overview of entity framework by software outsourcing company india

Prepared By:Mr. Dhrumil Patel

iFour Consultancy

Entity Framework

Page 2: Overview of entity framework by software outsourcing company india

Writing and managing ADO.Net code for data access is a tedious and monotonous job. Microsoft has provided an O/RM framework called "Entity Framework" to automate database related activities for your application.

The Microsoft ADO.NET Entity Framework is an Object/Relational Mapping (ORM) framework that enables developers to work with relational data as domain-specific objects, eliminating the need for most of the data access plumbing code that developers usually need to write. Using the Entity Framework, developers issue queries using LINQ, then retrieve and manipulate data as strongly typed objects. The Entity Framework's ORM implementation provides services like change tracking, identity resolution, lazy loading, and query translation so that developers can focus on their application-specific business logic rather than the data access fundamentals.

Entity Framework

Application Development Company Indiahttp://www.ifourtechnolab.com

Page 3: Overview of entity framework by software outsourcing company india

The Entity Framework is a mature ORM from Microsoft and can be used with a wide variety of databases.

There are three approaches to modeling entities using Entity Framework:Code FirstDatabase FirstModel First

Entity Framework Approaches

Application Development Company Indiahttp://www.ifourtechnolab.com

Page 4: Overview of entity framework by software outsourcing company india

The Code First approach helps you to create the entities in your application by focusing on the domain requirements.

In essence, you can follow Domain Driven Design (DDD) using this approach. Once your entities have been defined and the configurations specified, you can create the database on the fly using both.

The Code First approach gives you more control over your code -- you don't need to work with auto generated code anymore.

If you have the domain classes ready, you can easily create your database from the domain classes.

Code First

Application Development Company Indiahttp://www.ifourtechnolab.com

Page 5: Overview of entity framework by software outsourcing company india

The downside to this approach is that any changes to the underlying database schema would be lost; in this approach your code defines and creates the database.

The Code First approach allows you to use Entity Framework and define the entity model sans the designer or XML files.

You can use the POCO (Plain Old CLR Objects) approach to define the model and generate your database. In this approach you would typically create the entity classes. Here's an example; a typical entity class is

given below:public class Product { public int ProductId { get; set; } public string ProductName { get; set; } public float Price { get; set; } }

Code First (Cont.)

Application Development Company Indiahttp://www.ifourtechnolab.com

Page 6: Overview of entity framework by software outsourcing company india

Next, you should define a custom data context by extending the DbContext class as shown below:

public class IDGContext : DbContext { public DbSet<Product> Products { get; set; } }

Lastly, you should specify the connection string in the configuration file.

Code First (Cont.)

Application Development Company Indiahttp://www.ifourtechnolab.com

Page 7: Overview of entity framework by software outsourcing company india

You can use the Database First approach if the database is already designed and is ready. In this approach, the Entity Data Model (EDM) is created from the underlying database. As an example, you use the database first approach when you generate the edmx files in

the Visual Studio IDE from the database. Manual changes to the database is possible easily and you can always update the EDM if

need be (for example, if the schema of the underlying database changes). To do this, simply update the EDM from the database in the Visual Studio IDE.

Database First

Application Development Company Indiahttp://www.ifourtechnolab.com

Page 8: Overview of entity framework by software outsourcing company india

In the Model First approach you can create the EDM first, then generate the database from it.

You would typically create an empty EDM using the Entity Data Model Wizard in Visual Studio, define the entities and their relationships in Visual Studio, then generate the database from this defined model.

You can easily create entities and define their relationships and associations in the designer in Visual Studio. You can also specify the Key property and the data types for the properties for your entities using the designer. You can use partial classes to implement additional features in your entities.

Model First

Application Development Company Indiahttp://www.ifourtechnolab.com

Page 9: Overview of entity framework by software outsourcing company india

OK, but when should you use the Model First approach? Well, if neither the domain classes nor the database is ready and you would rather define the data model using a visual designer, this approach is for you. However, like in the Code First approach, in the Model First approach manual changes to the database would be lost as the model defines the database.

Model First (Cont.)

Application Development Company Indiahttp://www.ifourtechnolab.com

Page 10: Overview of entity framework by software outsourcing company india

Entity framework supports three types of relationships, same as database:One to OneOne to ManyMany to Many

Entity Relationships

Application Development Company Indiahttp://www.ifourtechnolab.com

Page 11: Overview of entity framework by software outsourcing company india

As you can see in the above figure, Student and StudentAddress have a One-to-One relationship (zero or one).

A student can have only one or zero address. Entity framework adds Student navigation property into StudentAddress entity and StudentAddress navigation entity into Student entity.

Also, StudentAddress entity has StudentId property as PrimaryKey which makes it a One-to-One relationship. As you can see in the below code, Student entity class includes StudentAddress navigation

property and StudentAddress includes Student navigation property with foreign key property StudentId. This way EF handles one-to-one relationship between entities.

One-To-One Relationship

Application Development Company Indiahttp://www.ifourtechnolab.com

Page 12: Overview of entity framework by software outsourcing company india

Example:public partial class Student{

public Student(){

this.Courses = new HashSet<Course>(); } public int StudentID { get; set; } public string StudentName { get; set; } public Nullable<int> StandardId { get; set; } public byte[] RowVersion { get; set; } public virtual Standard Standard { get; set; } public virtual StudentAddress StudentAddress { get; set; } public virtual ICollection<Course> Courses { get; set; }

}

One-To-One Relationship (Cont.)

Application Development Company Indiahttp://www.ifourtechnolab.com

Page 13: Overview of entity framework by software outsourcing company india

public partial class StudentAddress{

public int StudentID { get; set; } public string Address1 { get; set; } public string Address2 { get; set; } public string City { get; set; } public string State { get; set; } public virtual Student Student { get; set; }

}

One-To-One Relationship (Cont.)

Application Development Company Indiahttp://www.ifourtechnolab.com

Page 14: Overview of entity framework by software outsourcing company india

The Standard and Teacher entities have a One-to-Many relationship marked by multiplicity where 1 is for One and * is for many. This means that Standard can have many Teachers whereas Teacher can associate with only one Standard.

To represent this, The Standard entity has the collection navigation property Teachers (please notice that it's plural), which indicates that one Standard can have a collection of Teachers (many teachers). And Teacher entity has a Standard navigation property (Not a Collection) which indicates that Teacher is associated with one Standard. Also, it contains StandardId foreign key (StandardId is a PK in Standard entity). This makes it One-to-Many relationship.

One-To-Many Relationship

Application Development Company Indiahttp://www.ifourtechnolab.com

Page 15: Overview of entity framework by software outsourcing company india

Example:public partial class Standard{

public Standard() { this.Students = new HashSet<Student>(); this.Teachers = new HashSet<Teacher>(); } public int StandardId { get; set; } public string StandardName { get; set; } public string Description { get; set; } public virtual ICollection<Student> Students { get; set; } public virtual ICollection<Teacher> Teachers { get; set; }

}

One-To-Many Relationship (Cont.)

Application Development Company Indiahttp://www.ifourtechnolab.com

Page 16: Overview of entity framework by software outsourcing company india

public partial class Teacher{

public Teacher() { this.Courses = new HashSet<Course>(); } public int TeacherId { get; set; } public string TeacherName { get; set; } public Nullable<int> StandardId { get; set; } public Nullable<int> TeacherType { get; set; } public virtual ICollection<Course> Courses { get; set; } public virtual Standard Standard { get; set; }

}

One-To-Many Relationship (Cont.)

Application Development Company Indiahttp://www.ifourtechnolab.com

Page 17: Overview of entity framework by software outsourcing company india

Student and Course have Many-to-Many relationships marked by * multiplicity. It means one Student can enrol for many Courses and also, one Course can be be taught to many Students.

The database design includes StudentCourse joining table which includes the primary key of both the tables (Student and Course table). Entity Framework represents many-to-many relationships by not having entityset for the joining table in CSDL, instead it manages this through mapping.

Many-to-Many Relationship

Application Development Company Indiahttp://www.ifourtechnolab.com

Page 18: Overview of entity framework by software outsourcing company india

Example:public partial class Student{

public Student() { this.Courses = new HashSet<Course>(); } public int StudentID { get; set; } public string StudentName { get; set; } public Nullable<int> StandardId { get; set; } public byte[] RowVersion { get; set; } public virtual Standard Standard { get; set; } public virtual StudentAddress StudentAddress { get; set; } public virtual ICollection<Course> Courses { get; set; }

}

Many-To-Many Relationship (Cont.)

Application Development Company Indiahttp://www.ifourtechnolab.com

Page 19: Overview of entity framework by software outsourcing company india

public partial class Course{

public Course() { this.Students = new HashSet<Student>(); } public int CourseId { get; set; } public string CourseName { get; set; } public System.Data.Entity.Spatial.DbGeography Location { get; set; } public Nullable<int> TeacherId { get; set; } public virtual Teacher Teacher { get; set; } public virtual ICollection<Student> Students { get; set; }

}

Many-To-Many Relationship (Cont.)

Application Development Company Indiahttp://www.ifourtechnolab.com

Page 20: Overview of entity framework by software outsourcing company india

Thank you

Software development company indiahttp://www.ifourtechnolab.com