Showing posts with label ENTITY FRAMEWORK. Show all posts
Showing posts with label ENTITY FRAMEWORK. Show all posts
Friday, September 18, 2015

Order of Events in Windows Forms

.NET Framework 4.6 and 4.5
 
The order in which events are raised in Windows Forms applications is of particular interest to developers concerned with handling each of these events in turn. When a situation calls for meticulous handling of events, such as when you are redrawing parts of the form, an awareness of the precise order in which events are raised at run time is necessary. This topic provides some details on the order of events during several important stages in the lifetime of applications and controls. For specific details about the order of mouse input events, see Mouse Events in Windows Forms. For an overview of events in Windows Forms, see Events Overview (Windows Forms). For details about the makeup of event handlers, see Event Handlers Overview (Windows Forms).

Application Startup and Shutdown Events

The Form and Control classes expose a set of events related to application startup and shutdown. When a Windows Forms application starts, the startup events of the main form are raised in the following order:
When an application closes, the shutdown events of the main form are raised in the following order:
Monday, September 1, 2014

Example for update database when your project have two or more available context



Refer the below image for update database when your project have two or more available context



For example when your project have migration called context1 (Refer above image "For Context 1" statement)
update-database -configuration Sarasa.ERP.Data.Migrations.Configuration -force -verbose
in this statement the red colored text is the migration folder name for context1.

So when you want to update database within the context2 you only have to change the migration floder name in above statement, looks like example below.  (Refer above image "For Context 2" statement)
update-database -configuration Sarasa.ERP.Data.Migrations2.Configuration -force -verbose
Wednesday, August 20, 2014

Entity Framework 6 Code First Migrations with Multiple Data Contexts

Entity Framework 6 Code First Migrations with Multiple Data Contexts

Entity Framework code first migrations allows you to create a new database or to update existing database based on your model classes. Entity Framework5 code first migrations is only able to manage a single DbContext per physical database instance. Now, Entity Framework6 code first migrations is able to manage multiple DbContext per physical database instance. Let's see how to make it possible using EF6.
Suppose you have two DbContexts DataContext and UserDataContext. You need to migrates these two into single database instance.

Model Classes

public class User
{
 public int UserID { set; get; }
 public string FirstName { set; get; }
 public string LastName { set; get; }
}

public class Role
{
 public int RoleID { set; get; }
 public string RolesName { set; get; }
} 

public class Order
{
 public int OrderID { set; get; }
 public int Quantity { set; get; }
 public double Amount { set; get; }
 public DateTime Date { set; get; }
}

DbContexts

//first DbContext
namespace MultiDataContextMigrations.Models
{
public class DataContext : DbContext
{
 public DataContext()
 : base("DefaultConnection")
 {

 }

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
 {
 //TODO:Define mapping
 }

 public DbSet Users { get; set; }
 public DbSet Orders { get; set; }
}
}

//second DbContext
namespace MultiDataContextMigrations.Models
{
public class UserDataContext : DbContext
{
 public UserDataContext():base("DefaultConnection")
 {

 }
 
 protected override void OnModelCreating(DbModelBuilder modelBuilder)
 {
 //TODO:Define mapping
 }
 
 public DbSet Users { get; set; }
 public DbSet Roles { get; set; }
}
}

Case 1 : Multiple DbContexts within a Project

Suppose you have both DbContexts within a single project as shown in fig.
For migrating these two DbContexts to single database instance run the following commands by using VS2013 or VS2012 package manager console as given below:

Syntax - EF Code First Migrations with Multiple DbContexts within same Project

enable-migrations -ContextTypeName <DbContext-Name-with-Namespaces> -MigrationsDirectory:<Migrations-Directory-Name>
Add-Migration -configuration <DbContext-Migrations-Configuration-Class-with-Namespaces> <Migrations-Name>
Update-Database -configuration <DbContext-Migrations-Configuration-Class-with-Namespaces> -Verbose

Migrating First DbContext(DataContext)

enable-migrations -ContextTypeName MultiDataContextMigrations.Models.DataContext -MigrationsDirectory:DataContextMigrations
Add-Migration -configuration MultiDataContextMigrations.DataContextMigrations.Configuration Initial
Update-Database -configuration MultiDataContextMigrations.DataContextMigrations.Configuration -Verbose
When you run above listed command on the Package Manager Console windows one by one then:
  1. Firstly a Migrations folder will be added into your applications having Configuration.cs file for Migrations configuration setting.
    internal sealed class Configuration : DbMigrationsConfiguration
    {
     public Configuration()
     {
     AutomaticMigrationsEnabled = false;
     //Helps to migrate this DbContext to database
     MigrationsDirectory = @"DataContextMigrations"; 
     }
    
     protected override void Seed(MultiDataContextMigrations.Models.DataContext context)
     {
     // This method will be called after migrating to the latest version.
     }
    }
    
  2. Secondly a class file will be created having name suffix as MigrationsName followed by underscore and a unique generated number. This file will have all the entities to be created in the database as shown below:
    public partial class Initial : DbMigration
    {
     public override void Up()
     {
     CreateTable(
     "dbo.Orders",
     c => new
     {
     OrderID = c.Int(nullable: false, identity: true),
     Quantity = c.Int(nullable: false),
     Amount = c.Double(nullable: false),
     Date = c.DateTime(nullable: false),
     })
     .PrimaryKey(t => t.OrderID);
     
     CreateTable(
     "dbo.Users",
     c => new
     {
     UserID = c.Int(nullable: false, identity: true),
     FirstName = c.String(),
     LastName = c.String(),
     })
     .PrimaryKey(t => t.UserID);
     
     }
     
     public override void Down()
     {
     DropTable("dbo.Users");
     DropTable("dbo.Orders");
     }
    }
    
  3. Thirdly, a new database will be created having initial catalog name (initial catalog = YourDBName) as given in your connections string.

Migrating Second DbContext(DataContext)

enable-migrations -ContextTypeName MultiDataContextMigrations.Models.UserDataContext -MigrationsDirectory:UserDataContextMigrations
Add-Migration -configuration MultiDataContextMigrations.UserDataContextMigrations.Configuration Initial
Update-Database -configuration MultiDataContextMigrations.UserDataContextMigrations.Configuration -Verbose
When you run above listed command on the Package Manager Console windows one by one then:
  1. Firstly a Migrations folder will be added into your applications having Configuration.cs file for Migrations configuration setting.
    internal sealed class Configuration : DbMigrationsConfiguration
    {
     public Configuration()
     {
     AutomaticMigrationsEnabled = false;
     //Helps to migrate this DbContext to database
     MigrationsDirectory = @"UserDataContextMigrations";
     }
    
     protected override void Seed(MultiDataContextMigrations.Models.UserDataContext context)
     {
     // This method will be called after migrating to the latest version.
     }
    }
    
  2. Secondly a class file will be created having name suffix as MigrationsName followed by underscore and a unique generated number. This file will have all the entities to be created in the database as shown below:
    public partial class Initial : DbMigration
    {
     public override void Up()
     {
     CreateTable(
     "dbo.Roles",
     c => new
     {
     RoleID = c.Int(nullable: false, identity: true),
     RolesName = c.String(),
     })
     .PrimaryKey(t => t.RoleID);
     
     CreateTable(
     "dbo.Users",
     c => new
     {
     UserID = c.Int(nullable: false, identity: true),
     FirstName = c.String(),
     LastName = c.String(),
     })
     .PrimaryKey(t => t.UserID);
     
     }
     
     public override void Down()
     {
     DropTable("dbo.Users");
     DropTable("dbo.Roles");
     }
    }
    
  3. Before running update command, commented out the generated code for Users tables as shown above. Since Users table is already created by first DbContext migrations.
    public partial class Initial : DbMigration
    {
     public override void Up()
     {
     CreateTable(
     "dbo.Roles",
     c => new
     {
     RoleID = c.Int(nullable: false, identity: true),
     RolesName = c.String(),
     })
     .PrimaryKey(t => t.RoleID);
     
     //CreateTable(
     // "dbo.Users",
     // c => new
     // {
     // UserID = c.Int(nullable: false, identity: true),
     // FirstName = c.String(),
     // LastName = c.String(),
     // })
     // .PrimaryKey(t => t.UserID);
     
     }
     
     public override void Down()
     {
     DropTable("dbo.Users");
     DropTable("dbo.Roles");
     }
    }
    
    Now, run the third command then it will update the already created database by first DbContext migrations.

__MigrationHistory table

This table will contain all the migrations changes to database. Let's have a look on this table. In this way, you have successfully migrated both DbContexts to same database within SQL Server.

Undo/Rollback DbContexts Migrations

You can also rollback database changes by running following set of commands for both the DbContexts.
Update-Database -configuration MultiDataContextMigrations.DataContextMigrations.Configuration -TargetMigration:"201402141616393_Initial" -verbose
Update-Database -configuration MultiDataContextMigrations.UserDataContextMigrations.Configuration -TargetMigration:"201402141641408_Initial" -verbose

Case 2 : Multiple DbContexts within different Projects

Suppose you have both DbContexts within different projects as shown in fig.
For migrating these two DbContexts to single database instance run the following commands by using VS2013 or VS2012 package manager console as given below:

Syntax - EF Code First Migrations with Multiple DbContexts within different Projects

enable-migrations -ProjectName:<ProjectName> -MigrationsDirectory:<Migrations-Directory-Name>
add-migration <Migrations-Name> -ProjectName:<ProjectName>
update-database -ProjectName:<ProjectName> -verbose

Migrating First DbContext(DataContext)

//migrating DataContext
enable-migrations -ProjectName:DAL -MigrationsDirectory:DataContextMigrations
add-migration InitialCreate -ProjectName:DAL
update-database -ProjectName:DAL -verbose

Migrating Second DbContext(UserDataContext)

//migrating UserDataContext
enable-migrations -ProjectName:MultiDataContextMigrations -MigrationsDirectory:UserDataContextMigrations
add-migration InitialCreate -ProjectName:MultiDataContextMigrations
update-database -ProjectName:MultiDataContextMigrations -verbose

Undo/Rollback DbContexts Migrations

You can also rollback database changes by running following set of commands for both the DbContexts.
update-database -ProjectName:DAL -TargetMigration:"201401290826231_InitialCreate" -verbose
update-database -ProjectName:MultiDataContextMigrations -TargetMigration:"201401290836540_InitialCreate" -verbose
**********************************************************************
After you want to update database you have
 to use this commend
update-database -configuration MigrationFolder.Configuration -Verbose