Tuesday, December 10, 2013

Database Initialization

Let’s how database initialize in code-first application.

Following figure shows database initialization workflow based on the parameter passed in base constructor of context class which is derived from DbContext:

Entity Framework code-first database initialization
As per above figure, you can pass following parameters in the base constructor:

No Parameter: If you do not pass the parameter then it creates the database in your local SQLEXPRESS with name matches your {Namespace}.{Context class name}. eg. SchoolDataLayer.Context for following context:
     
    public class Context: DbContext 
    {
        public Context(): base()
        {
            
        }
    }
        
Name: If you pass “Name” parameter then it creates database in the local SQLEXPRESS db server using that name. For example: below code will create “SchoolDB-CodeFirst” database
    
    public class Context: DbContext 
    {
        public Context(): base("SchoolDB-CodeFirst") 
        {
                   
        }
    }
        
ConnectionStringName: If you pass connection string name of app.config or web.config then it will create the database as per connection string. For example, below code will create "SchoolDB-ByConnectionString" database:
   
    public class Context: DbContext 
    {
        public SchoolDBContext() : base("SchoolDBConnectionString") 
        {
        }
    }
        
App.config:
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <connectionStrings>
        <add name="SchoolDBConnectionString" 
        connectionString="Data Source=.;Initial Catalog=SchoolDB-ByConnectionString;Integrated Security=true" 
        providerName="System.Data.SqlClient"/>
      </connectionStrings>
    </configuration>
        
Above Context class sends connection string name as a parameter. So it will create new "SchoolDB-ByConnectionString" database or use existing “SchoolDB-ByConnectionString” database at local SQL Server (because we have mentioned '.' not SQLEXPRESS). Make sure that you include providerName = "System.Data.SqlClient" in the connection string.

No comments:

Post a Comment