HI WELCOME TO SIRIS
Showing posts with label entity framework. Show all posts
Showing posts with label entity framework. Show all posts

Difference between LINQ to SQL and Entity Framework

LINQ to SQL allow you to query and modify SQL Server database by using LINQ syntax. Entity framework is a great ORM shipped by Microsoft which allow you to query and modify RDBMS like SQL Server, Oracle, DB2 and MySQL etc. by using LINQ syntax. Today, EF is widely used by each and every .NET application to query to database. The difference between LINQ to SQL and EF is given below.
LINQ to SQL
Entity Framework
It only works with SQL Server Database.
It can works with various databases like Oracle, DB2, MYSQL, SQL Server etc.
It generates a .dbml to maintain the relation
It generates an .edmx files initially. The relation is maintained using 3 different files .csdl, .msl and .ssdl
It has not support for complex type.
It has support for complex type.
It cannot generate database from model.
It can generate database from model.
It allows only one to one mapping between the entity classes and the relational tables /views.
It allows one-to-one, one-to-many & many-to-many mappings between the Entity classes and the relational tables /views
It allows you to query data using DataContext.
It allows you to query data using EntitySQL, ObjectContext, DbContext.
It provides a tightly coupled approach.
It provides a loosely coupled approach. Since its code first approach allow you to use Dependency Injection pattern which make it loosely coupled .
It can be used for rapid application development only with SQL Server.
It can be used for rapid application development with RDBMS like SQL Server, Oracle, DB2 and MySQL etc.
What do you think?
I hope you will enjoy the LINQ to SQL and EF while playing with database. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Using T4 templates in Entity framework

T4 stands for Text template transformation toolkit which is a template-based code generation engine built into Visual Studio. It is available in Visual studio from Visual Studio 2008 and higher version of Visual Studio. T4 engine allows you to generate C#, T-SQL, XML or any other text files by using ASP.NET – ASPX template like syntax. T4 template has .tt extension.
For example, TextTemplate.tt T4 template has the following code:
  1. <#@ template language="C#" #>
  2. <#@ assembly name="System.Core" #>
  3. <#@ import namespace="System.Text" #>
  4. <#@ output extension=".txt" #>
  5. <# Write("T4 templates with EF!"); #>
The T4 template processor will transform above T4 template code into a text file having extension .txt by executing the code and processing directives inside.
  1. T4 templates with EF!

T4 templates with Entity framework

T4 templates in entity framework are used to generate C# or VB entity classes from EDMX files. Visual Studio 2013 or 2012 provides two templates- EntityObject Generator and DBContext Generator for creating C# or VB entity classes. The additional templates are also available for download.
To use one of these templates, right-click on the design surface of an .edmx file and select the "Add Code Generation Item" command as given below-


Selecting the command will launch a dialog box allowing you to select one of the installed code-generation items, or to search for new items online.


Selected template will generate the C# or VB code files i.e. entity and context classes as given below.

What do you think?
I hope you will enjoy the T4 template while working with EF. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Various Domain modelling approaches in Entity Framework

A business domain is populated with related and interconnected entities which have its own properties and behavior. Most important thing is that each entity may have a state and can be bound to a possibly dynamic list of validation rules. EF allow developers to focus on the business domain and to model it in terms of classes.

Domain modelling approaches

There are three approaches of domain modelling which was introduced with Entity Framework 4.1
  1. Code First
  2. Model First
  3. Database first

Code First

Code first is the domain modelling approach in Entity Framework. It enables you to describe a model by using C# or VB.NET classes and then create database from these classes. These classes are called POCO classes.
This approach enables us to work entirely in an object-oriented direction, and not worry about the structure of the database. This abstraction allow us to make a more logically and flexible application that focuses on the behaviour of the application rather than the database generated by it.

Advantages of Code First

  1. It is very popular approach since it allow you to make a more logically and flexible application.
  2. It provides full control over the code since there is no auto generated code which is hard to modify.
  3. In this approach, your code defines only the database mappings and EF will handle creation of database with its relations.
  4. Manual changes to database schema is not preferable because your code defines the database.
  5. You can also use Code first to map your model to an existing database.

Model First

Model first is the domain modelling approach in Entity Framework. It enables you to create model’s Entities, relationships, and inheritance hierarchies on the design surface of empty model (.edmx file) by using entity designer and then create database from it. This approach is adopted by the architect and solution lead developers.
In Model First approach, while creating Entity Data Model, you must select the option “Empty Model” instead of "Generate from database" option.

Advantage of Model First

  1. It is good if you like to visualize the structure of the data in the application or you don't like writing code or SQL since it will generate for you.
  2. In this approach you have no much control over your entities (auto generated code which is hard to modify) and database. In this way it is rarely used but for small easy projects this approach will be very productive.
  3. To add extra features in POCO entities you must either T4 modify template or use partial classes.
  4. Manual changes to database schema is not preferable because your model defines the database.

Database First

Database first is the domain modelling approach in Entity Framework. It enables you to create model from an existing database (like SQL Server, Oracle, DB2 etc.). This approach reduces the amount of code that we need to write since it automatically generates code. But it also limits us to work with the structure of the generated code.

Advantage of Database First

  1. Very popular if you have Database designed by DBAs, developed separately or if you have existing Database.
  2. EDM wizard creates entities, relationships, and inheritance hierarchies for you. After modification of mapping, you can also generate POCO class entities.
  3. To add extra features in POCO entities you must either T4 modify template or use partial classes.
  4. Manual changes to the database are possible because the database defines your domain model. You can always update model from database.
What do you think?
I hope you will enjoy the domain modeling approaches while programming with EF. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Understanding Inheritance in Entity Framework

Inheritance in the Entity Framework is similar to inheritance for classes in C#. In Entity Framework, you can map an inheritance hierarchy to single or multiple database tables based on your requirements. In this article, you will learn how to map your inheritance hierarchy to database tables in SQL Server.

Types of inheritance in Entity Framework

Entity Framework supports three types of inheritances as given below-
  1. Table-per-Hierarchy (TPH)

    The TPH inheritance states that all entities, in a hierarchy of entities, are mapped to a single table in storage schema. It means, there is only one table in database and different Entity types in Entity model that inherits from a base Entity are mapped to that table.

    C# Implementation Code for TPH

    1. public class Course
    2. {
    3. [Key]
    4. public int CourseId { get; set; }
    5. public string Name { get; set; }
    6. public string Details { get; set; }
    7. public string Trainer { get; set; }
    8. }
    9.  
    10. public class OnlineCourse : Course
    11. {
    12. public string URL { get; set; }
    13. }
    14.  
    15. public class OfflineCourse : Course
    16. {
    17. public string Address { get; set; }
    18. }

    Entity Framework Code First Mapping for TPH

    1. modelBuilder.Entity<Course>()
    2. .Map<OnlineCourse >(m => m.Requires("Type").HasValue("OnlineCourse "))
    3. .Map<OfflineCourse >(m => m.Requires("Type").HasValue("OfflineCourse "));

    Note

    By default, Entity Framework supports TPH inheritance, if you don't define any mapping details for your inheritance hierarchy.
  2. Table-per-Concrete-Type (TPC)

    The TPC inheritance states that each concrete class (a class which can be instantiated) in the hierarchy of entities is mapped to a separate table in storage schema. It means, there is separate table in database to maintain data for each derived entity type.

    C# Implementation Code for TPC

    1. public abstract class Course //abstract class
    2. {
    3. [Key]
    4. public int CourseId { get; set; }
    5. public string Name { get; set; }
    6. public string Details { get; set; }
    7. public string Trainer { get; set; }
    8. }
    9.  
    10. public class OnlineCourse : Course //concrete class
    11. {
    12. public string URL { get; set; }
    13. }
    14.  
    15. public class OfflineCourse : Course //concrete class
    16. {
    17. public string Address { get; set; }
    18. }

    Entity Framework Code First Mapping for TPC

    1. modelBuilder.Entity<OnlineCourse >().Map(m =>
    2. {
    3. m.MapInheritedProperties();
    4. m.ToTable("OnlineCourse ");
    5. });
    6.  
    7. modelBuilder.Entity<OfflineCourse >().Map(m =>
    8. {
    9. m.MapInheritedProperties();
    10. m.ToTable("OfflineCourse ");
    11. });

    Note

    The TPC inheritance is commonly used to inherit basic features.
  3. Table-per-Type (TPT)

    The TPT inheritance states that each entity in the hierarchy of entities is mapped to a separate table in storage schema. It means, there is separate table in database to maintain data for each Entity Type.

    C# Implementation Code for TPT

    1. public class Course
    2. {
    3. [Key]
    4. public int CourseId { get; set; }
    5. public string Name { get; set; }
    6. public string Details { get; set; }
    7. public string Trainer { get; set; }
    8. }
    9.  
    10. public class OnlineCourse : Course
    11. {
    12. public string URL { get; set; }
    13. }
    14.  
    15. public class OfflineCourse : Course
    16. {
    17. public string Address { get; set; }
    18. }

    Entity Framework Code First Mapping for TPT

    1. modelBuilder.Entity<Course>().ToTable("Course");
    2. modelBuilder.Entity<OnlineCourse >().ToTable("OnlineCourse ");
    3. modelBuilder.Entity<OfflineCourse >().ToTable("OfflineCourse ");

    Note

    TPH inheritance patterns generally deliver better performance in the Entity Framework than TPT inheritance patterns, because TPT patterns can result in complex join queries.
What do you think?
I hope you will enjoy the tips while programming with Entity Framework. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.