Category Archives: Entity FrameWork

Entity Framework Category

Code First Default Value – Easy way

There is a easy way to set default values in Entity Framework   Code first. all you need to do is just set your default value in your constructor. after all entity is also behave like an object. below example show how to set default value of IsActive Property

 public class MaterialCategory

   public MaterialCategory()
   public int Id { get; set; }
   public string Name {get;set; }
   public string Description { get; set; }
   public bool IsActive { get;set;}


Join Example in Entity framework and Linq

  using (var context = new DataBaseEntities())
  {          var data = from emp in context.Employees
                       join dpt in context.Departments on emp.DepartmentId equals dpt.Id
                       where dpt.Id == 1
                       select new { epf = emp.EPFNumber, dept_name = dpt.DepartmentName };

Dynamically Build Entity Framework Connection At Runtime

sometimes you may need to build your entity framework based connection dynamically during the run time instead of using app.config file.

note that you need to add following using statements

using System.Data.SqlClient;
using System.Data.EntityClient;
using System.Data.Metadata.Edm;

using System.Data.SqlClient;
using System.Data.EntityClient;
using System.Data.Metadata.Edm;

 internal static EntityConnection GetConnection()
            // Specify the provider name, server and database.
            string providerName = "System.Data.SqlClient";
            string serverName = GetServerName() ;
            string databaseName = GetDatabaseName();

            // Initialize the connection string builder for the
            // underlying provider.
            SqlConnectionStringBuilder sqlBuilder =
            new SqlConnectionStringBuilder();

            // Set the properties for the data source.
            sqlBuilder.DataSource = serverName;
            sqlBuilder.InitialCatalog = databaseName;
            sqlBuilder.UserID = userID;
            sqlBuilder.Password = password;

            // Build the SqlConnection connection string.
            string providerString = sqlBuilder.ToString();

            // Initialize the EntityConnectionStringBuilder.
            EntityConnectionStringBuilder entityBuilder =
            new EntityConnectionStringBuilder();

            //Set the provider name.
            entityBuilder.Provider = providerName;

            // Set the provider-specific connection string.
            entityBuilder.ProviderConnectionString = providerString;

            // Set the Metadata location.
            entityBuilder.Metadata = @"res://*/MetaModel.csdl|

            EntityConnection conn = new EntityConnection(entityBuilder.ToString());

            return conn;


Usage Of this Method

       using (var contxt = new YourDatabaseEntities(GetConnection()))
         // your code go here.       

Happy Coding.. !!

Sort Data using Entity Framework

sometimes we need to sort data when working with entity sets. here is very simple yet powerful single line statement  that sorts data.

we are going to sort data using CustomerName Property. remember that “it” is a must.

//Ascending order
var data = context.Customers.OrderBy(“it.CustomerName ASC”);

//descending order
var data = context.Customers.OrderBy(“it.CustomerName DESC”);

// lets user this data
customerBindingSource.DataSource = data;