Basic Entity Framework Questions & Answers

What is Entity Framework ?
It is mediator between database & an application that enables the developers to program against the conceptual application model instead of programming directly against the relational storage schema . Basically it generates data access logic and entities according of the database tables.

What is EDMX file in Entity Framework ?
EDMX (entity data model xml) is xml file which has all the mapping details between objects & sql tables.
It is divided into three part CSDL,SSL,MSL.
entity framework

What is CSDL ?
CSDL (Conceptual Schema Definition Language ) is XML based language which describes entities, relationships & functions that make up a conceptual model for data-driven application.
(It creates conceptual model for an application.)

What is SSDL ?
SSDL (Storage Schema Definition Language) is a XML based language that describes the storage model of an Entity Framework application.
(It defines mapping details with database)

What is MSL ?
MSL (Mapping Specification Language) is a XML based language that describes the mapping between the conceptual model and storage model of an Entity Framework application.
(it connects CSDL with SSDL)

What is t4 template ?
It template which generates entity and context classes for entity framework which will be used by developer.

What is Lazy Loading ?
Refer :

What is Code first approch in Entity Framework ?
Code first menas first we our own Entity classes & then crete database from these classes.

How to create database using code first approch
Lets create two entity classes

public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
public virtual List Posts { get; set; }
}

public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
}

Now After this we will create its context class

public class BloggingContext : DbContext
{
public DbSet Blogs { get; set; }
public DbSet Posts { get; set; }
}

Now when we use this entities, database will be automatically generated.

How to make filed as primary key if it does not follow code first conversion ?
Lets say in above code I have Field name “BlogUnique” Instead of “Id” then EF will not be able to identify which is its primary key.
So to make EF understand we need to add attribute “[key]” for the field which we want to make primary.

public class Blog
{
[key]
public int BlogUnique { get; set; }
public string Name { get; set; }
public virtual List Posts { get; set; }
}

public class Post
{
[key]
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
}

Now how to make foreign key in Post table for “BlogUnique” key ?
To do this we have to explicitly define relation in Post table like bellow

public class Blog
{
[key]
public int BlogUnique { get; set; }
public string Name { get; set; }
public virtual List Posts { get; set; }
}

public class Post
{
[key]
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }

[ForeignKey(“BlogId”)]
public virtual Blog Blog {get;set;}
}

What are other attributes which can be used in model to make more better database ?
1. [Required]
This is used when you want to define any field as required in your database.

2. [MaxLength] & [MinLength]
You can define max & min length of string property/field using this attributes just like bellow

 [MaxLength(500, ErrorMessage="Maximum of 500 characters please")]
    [MinLength(10, ErrorMessage="Minimum of 10 characters required")]

3. [NotMapped]
If you dont want to create column of any property then you can put [NotMapped] attribute on it. Ef will not create column of this property.

[NotMapped]
public string BlogCode
{
   get
      {
           return “values”;
      }
}

4. [ComplexType]
It is used to create complex type properties just like bellow

[ComplexType]
public class BlogDetails
{
   public DateTime DateCreated{get;set;}
   public Description {get;set;}
}

This can be used in any other class just like bellow

public class Blog
{
[key]
public int BlogUnique { get; set; }
public string Name { get; set; }
public virtual List Posts { get; set; }
public BlogDetails Details{get;set;}
}

5. [Column]
It is used if you want name of database column different from property name.

[Column(“blogId”)]
public int BlogUnique { get; set; }

By doing this database column name of property “BlogUnique” will be BlogId.

6. [Table]
It is used if you want name of database table different from class name.

[Table(“BlogItems”)]
public class Blog
{
[key]
public int BlogUnique { get; set; }
public string Name { get; set; }
public virtual List Posts { get; set; }
}

This will create table name “BlogItems” for class “Blog”.

What is Database First Approch in Entiy Framework ?
It is approach by which we create entity classes from already created database.When there is any change in database, we have to update entity model of it.

Different approach of entity framework

EDMX Entity Context
Simple entity framework Needed Auto Auto
POCO approach Needed Manual Auto
Code First Not Needed Manual Manual

What are Scaler & Navigation properties in Entity Framework ?
Scaler Properties : They are actual values in entity. Normally a scalar property will map to a database field.
Navigation Properties : They reference to other objects.They helps us to navigate from one entity to another entity.

What are complex types in Entity Framework?
There can be situations where you have common properties (duplicate fields) across entities. So to remove these duplicate and redundant fields, we can move them to a common complex type. Complex types groups common properties so that they can be re used across entities.

Lets Take example : I have two tables Employee & Client

Enployee Table
Empl Id
First Name
Last Name

 

Client Table
ClientId
First Name
Last Name

Here First name & Last Name are common fields in both tables.
So We can Create complex type “Name“ which will have This two fields.

Name First Name Last Name

How to create complex types ?
To create a complex type, select the fields which you want to group in a complex type, click on Refactor, and create the complex type.
complex types

What is DbContext ?
DbContext is wrapper around ObjectContext & very easy to use.

What is Concurrency and how can we handle it in Entity Framework ?
Concurrency is an issue which can happen when 2 or more users tries to update same data at same time.
In entity framework is resolved by optimistic locking. To enable this just right click on EDMX designer and set the concurrency mode to “Fixed”.

Concurrency

Now when ever concurrency occurs it will give “OptimisticConcurrencyException” which can be handled using try catch block.

What is optimistic locking ?
It is strategy where you read a record & take a note of version number & check it while updating the same record.
If version is different then it will abort transaction.

What is Pessimistic Locking ?
It is strategy where you lock a record until you are finished with it.

admin

  • yassershaikh

    Nice compilation man !!! Can i use this on careerbaba ??

  • Vaibhav Shah

    Just give some credits to me. :P