Quantcast
Viewing all articles
Browse latest Browse all 9

NuoDB Support for Entity Framework Code First Migrations

Jiri Cincura

As Entity Framework simplifies working with data – and I mean basic operations like querying and updating – there’s also of course demand to simplify other parts of an application developer’s life. The data needs to be stored somewhere. Somewhere in a database with structure that, preferably, matches your Entity Framework model and classes!

Code First Migrations is the part of Entity Framework that makes it easier to manage the structure of a database as an application evolves over time. You don’t have to deal with DDL commands and so on; you just let the logic update your database to the current state of the model. And, of course, you can change the result before it’s applied and even add elements as you see fit, such as indexes.

(In the example that follows, I’ll outline a simple example of how to work with Migrations. Visit MSDN if you need help getting started with Entity Framework.)

If you install (or update) a freshly-released Entity Framework driver for NuoDB from NuGet by invoking the Tools | NuGet Package Manager | Package Manager Console and issuing the command Install-Package EntityFramework.NuoDb you’ll be ready to go.

Image may be NSFW.
Clik here to view.
Install Entity Framework

Let’s start with a simplified Entity Framework model, similar to the “Hockey” example:

class HockeyContext : DbContext
{public HockeyContext():base(new NuoDbConnection(Config.ConnectionString), true){} 
       protectedoverridevoid OnModelCreating(DbModelBuilder modelBuilder){base.OnModelCreating(modelBuilder); 
              modelBuilder.HasDefaultSchema("migrations"); 
              var playerConf = modelBuilder.Entity<Player>();
              playerConf.ToTable("PLAYERS");} 
       public DbSet<Player> Players {get;set;}} 
class Player
{publicstring PlayerId {get;set;}publicstring FirstName {get;set;}publicstring LastName {get;set;}}

The next step is to enable Migrations using the Enable-Migrations cmdlet, where I’ll use the supplied defaults. This step will create a new folder in the project, with a configuration file inside.

Image may be NSFW.
Clik here to view.
Enable-Migrations.png

Then I add a first migration using Add-Migration Initial (using, in this case, the name “Initial”). This creates a class with the same name, capturing the current state of the model:

publicpartialclass Initial : DbMigration
{publicoverridevoid Up(){
        CreateTable("migrations.PLAYERS",
            c =>new{
                    PlayerId = c.String(nullable:false, maxLength:128, unicode:false),
                    FirstName = c.String(unicode:false),
                    LastName = c.String(unicode:false),
                }).PrimaryKey(t => t.PlayerId);} 
    publicoverridevoid Down(){
        DropTable("migrations.PLAYERS");}}

Image may be NSFW.
Clik here to view.
First-Migration.png

This looks like C# code, but it’s not meant to be executed; it only helps to capture the migration steps, which will be translated to SQL for NuoDB using Update-Database -Script. This will open a new document containing the SQL that should be executed on the database in order to set up the tables corresponding to our data model:

CREATETABLE"migrations"."PLAYERS"("PlayerId"VARCHAR(128)NOTNULL,"FirstName"VARCHAR(32765),"LastName"VARCHAR(32765))
;
 
ALTERTABLE"migrations"."PLAYERS"ADDCONSTRAINT"PK_migrations_PLAYERS"PRIMARYKEY("PlayerId")
;
 
CREATETABLE"migrations"."__MigrationHistory"("MigrationId"VARCHAR(150)NOTNULL,"ContextKey"VARCHAR(300)NOTNULL,"Model"BLOBNOTNULL,"ProductVersion"VARCHAR(32)NOTNULL)
;
 
ALTERTABLE"migrations"."__MigrationHistory"ADDCONSTRAINT"PK_migrations___MigrationHistory"PRIMARYKEY("MigrationId","ContextKey")
;
 
INSERTINTO"migrations"."__MigrationHistory"("MigrationId","ContextKey","Model","ProductVersion")VALUES('201409211350349_Initial','NuoDbMigrationsTest.Migrations.Configuration', 0x1F8B0...0D0000,'6.1.1-30610')

Image may be NSFW.
Clik here to view.
Update-Database.png

This is a fairly typical SQL script based on our simple model. You can also make the framework apply the changes on your behalf by removing the -Script option and just invoking Update-Database.

Let’s add a new column and index it using CreateIndex:

publicpartialclass Index : DbMigration
{publicoverridevoid Up(){
        AddColumn("migrations.PLAYERS", "Snafu", c => c.Int(nullable:false));
        CreateIndex("migrations.PLAYERS", "Snafu", unique:false, name:"IDX_PLAYERS_Snafu");} 
    publicoverridevoid Down(){
        DropColumn("migrations.PLAYERS", "Snafu");}}

Here’s the resulting script:

ALTERTABLE"migrations"."PLAYERS"ADD"Snafu"INTNOTNULLDEFAULT0;
 
CREATEINDEX"IDX_PLAYERS_Snafu"ON"migrations"."PLAYERS"("Snafu");
 
INSERTINTO"migrations"."__MigrationHistory"("MigrationId","ContextKey","Model","ProductVersion")VALUES('201409211407156_Index','NuoDbMigrationsTest.Migrations.Configuration', 0x1F8B0800...8730F0000,'6.1.1-30610');

The magic behind this is in the “NuoDbMigrationSqlGenerator” class. You can derive your own if you’d like, to change the generated SQL. You’ll have to tell Entity Framework in the “DbConfiguration” derived class to use it, via the “SetMigrationSqlGenerator” method.

Easy, right? I hope you’ll enjoy this new Code First Migrations chapter in the Entity Framework story.  Give NuoDB and Entity Framework a spin.

Old nid for redirect: 
10316

Viewing all articles
Browse latest Browse all 9

Trending Articles