All posts by Julie

A Notable Entity Framework Performance Eater: Casting nchar to nvarchar under the covers

Brian Sullivan was having a weird query performance problem with one particular EF query and asked about it here on Stackoverflow. Some very knowledgeable people asked all the right questions. It made no sense.

He finally discovered the source of the problem himself.

His code first model defined a string property which SQL Server automatically presumes is an nvarchar. But his database had an varchar. Since these are coercible, Entity Framework decided to coerce the type under the covers. And it was very expensive. Brian fixed the problem by applying a mapping that just said “hey EF, that’s an varchar by the way” and all was well again.

This is one of those awful problems that will be hard to solve and hard to do a web search for. But hopefully the SO question or this blog post will help the next dev coming down the pipe.

Brian has written his own, more detailed blog post about this here -> Entity Framework Code-First Performance Issue with String Queries

Video Pick from Getting Started with Entity Framework 5 Course

Here is a 6 minute preview from my new Pluralsight course, Getting Started with Entity Framework 5.

 

The course is about 4.5 hours long and I’ve written a small “guide” to it in this blog post: Getting Started with EF5 – New Course on Pluralsight so that you can decide if the course is for you or not. But I think it’s for everyone!! 🙂

Or you can go right to the course here: http://pluralsight.com/training/Courses/TableOfContents/entity-framework5-getting-started

Or you can watch one of my 9 other Entity Framework courses on Pluralsight! 🙂 http://pluralsight.com/training/Authors/Details/julie-lerman

I’ve still got a short stack of 30 day trial cards for Pluralsight if you need one but you have to promise to use it to watch my courses! 🙂 (And any of the other awesome courses you can find there as well.) Send a message to me through my contact form and I can shoot you the code.

Entity Framework at Boston Code Camp #19

I had the pleasure of going to Cambridge this weekend along with a car load of Vermonters (myself, Rachel Reese, Dennis Doire and Kyle Mitofksy) to participate in Boston Code Camp at Microsoft’s NERD Center.

We left just as Boston was getting pummeled with snow and a prediction of 10-14”. We saw nary a flake on the drive down.

Rachel gave two F# presentations. Dennis gave a session on Specflow and Selenium. I did two talks on ..guess what? Yep Entity Framework.

 

Session #1 Entity Framework in the Enterprise

The first talk was Entity Framework in the Enterprise. It was a very compressed version of my 3+ hour course of the same name on Pluralsight: Entity Framework in the Enterprise.

You can find the slides for this session on Slideshare at http://www.slideshare.net/JulieLerman/lerman-ef-in-the-enterprise-bocc13.

I spent quite a lot of time preparing and testing the 6 solutions that I showed in the session. Afterwards a friend noted that I had never actually run any of the apps or test whose code I had spent most of my time exploring. That made me laugh because I had worked hard to ensure that every app ran correctly and every test passed. So to remedy that, I have created a short video on YouTube where you can actually see the tests run (and pass) and the one of the apps running. Here’s that video: Lerman EF in the Enterprise Run Tests BOSCC13 (there’s no sound and it’s about 4.5 minutes).

Session #2: EF FTQs (or Entity Framework, Frequently Tweeted Questions)

I have also put up the slides for the second session on Slideshare at http://www.slideshare.net/JulieLerman/julie-lerman-entity-framework-ftqs-frequently-tweeted-questions. Something strange happened during the demos. I wanted to show attendees the reverse engineering feature of the EF Power Tools. I pointed to a database and nothing happened…no classes were created. I’ve never seen that happen before. I didn’t see an error message on the bottom of the screen. I tried it again with the same results, so instead of doing what I wanted to which was get to the bottom of the problem, I knew it was better to just move on. I spent some time this morning trying to duplicate the problem and never could. So who knows what happened. So for those of you who haven’t seen this feature before, I recorded a little video (again, no sound and it’s only 78 seconds) so that you can see it in action. That’s here: Lerman Reverse Engineer Database for Code First

 

It was a great code camp! Congrats to all of the folks who made it happen: speakers, attendees, sponsors and organizers/volunteers…especially to Patrick Hynds, Bob Goodearl and John Zablocki and Chris Pels.

bostoncodecamp.com

Getting Started with EF5 – New Course on Pluralsight

While EF5 did not bring a huge amount of change to Entity Framework, one small change in the EF Designer in Visual Studio 2012 made a huge difference for people getting started with Entity Framework. The designer now uses a code generator that spits out POCO entity classes managed by a DbContext by default rather than the EntityObject based classes managed by the ObjectCpntext as it did for VS2008 and VS2010. It’s a VERY good change but because almost all of the training materials out there for EF newbies is based on VS2010, it is pretty confusing to get started with EF5 and VS2012.

The advanced materials are still completely valid.

While this is driving me a little crazy with respect to the 2nd edition of Programming Entity Framework – a huge undertaking to update, I was able to fix the problem by creating a brand new course on Pluralsight.

It’s just been released today and is called “Getting Started with Entity Framework 5”.

It’s got 6 modules.

Entity Framework 5: Planning Ahead

This is a good overview for newbies but also highlights “what’s new” which will be really helpful for people coming from EF4 or even EF4.1, 4.2 or 4.3.

Database First Modeling

Like using the designer and starting with an existing database? Database First is for you. Also, there are plenty of new designer features so if you’re experienced with EF but moving to VS2012, you’ll want to learn about the new features (including creating enums from database fields and supporting geography data types).

Model First Modeling

Like the designer but no database yet? You may prefer to go this route of designing your model in the designer and letting the designer generate database schema for you. While I touch on some of the new designer features in this module, I don’t pay as much attention to them as I do in the DB First module because it would just be much too repetitive. But you will also see here how to define enums and geography data that will properly map the database schema that the designer creates.

For both model first and database first, you can learn more about customizing your model in the course Designer Supported EDM Customization.

Code First Modeling

If this is you: “eew designers stink” then you probably want to just use code to define your model. This gives highlights of Code FIrst along with the new enums & spatial data support. You can then continue your education in the EF 4.1 Code First course and the Code First Migrations course.

Interacting with your Data Model

Whether you’ve created your model with Database First, Model First or Code First, using the model is almost always the same. (“Almost” because code first does not yet support mapping to stored procedures so that’s the only real difference.) This module uses a simple console application to get you going with the basics of querying and updating your database using the DbContext. Once you’ve got an understanding of these basics, the dedicated DbContext, Querying and Validation courses that I’ve created in the past few years will get you the in-depth info you need for more advanced work.

Using EF in Your Solutions

I didn’t want to leave you with the impression that what you saw in the previous module is as far as EF can go. This last module introduces you to some good architectural practices and then I use EF (along with these basic architecture patterns) in a client side application (WPF), a web app (MVC) and a data service so you can see how the pieces fit together. I have a much more advanced course called “Entity Framework in the Enterprise” that you can watch after this. That course was created using Visual Studio 2010 and Code First and the DbContext and everything in there will be applicable to EF5.

Enjoy!

http://pluralsight.com/training/Courses/TableOfContents/entity-framework5-getting-started

What’s Best for Unit Testing in EF? It depends, dude!

This tweet stopped me in my tracks because I couldn’t reply in a tweet:

Stephen Coulson @sdcoulson

@julielerman In your opinion what is the best solution for unit testing#EF dependent code?#MockingFramework? #SQLCE? or other?

It’s a loaded question so I’ll just answer briefly.

First: it depends! 🙂 It always depends.

Do you mean unit tests or integration tests? A unit test shouldn’t be hitting a database.

FAKING FOR UNIT TESTS

I use fakes to avoid hitting a database when I truly want to do a unit test on code that’s trying to do data access. I (and many others) have written extensively about faking with EF. I have some old blog posts and book chapters that do this with EF4 but not using code first or dbcontext.

In my DbContext book I have some examples of faking with DbContext (ala EF4.1 & EF5).

I have a module in one of my Pluralsight courses on testing with EF (Entity Framework in the Enterprise).

MOCKING FRAMEWORKS FOR UNIT TEST

I’ve had a hard time finding good current examples of using mocking frameworks with EF. Typemock has one that’s terribly outdated and not useful. I found one person who’s written a little about using Moq with EF.

SQL CE FOR INTEGRATION TESTS

Sure you can use SQL CE. It’s a perfectly good way to go. Here’s a blog post by Eric Hexter to get you started:

Using sql compact for integration tests with entity framework

CODE FIRST DATABASE INITIALIZATION FOR INTEGRATION TESTS

This is what I use. One of the intializers is “DropCreateDatabaseAlways”. I use that combined with some seed data to refresh my database for each integration test. This is one of the methods I show in my Pluralsight course.

More More More!

There are plenty of other ways to go as well. For example, Lynn Langit has pointed me via twitter to ApprovalTests.

And just watch the comments fill up with other suggestions.

Hope this helps, Stephen! 🙂

Attaching an MDF Database to LocalDb in Visual Studio the Easier Way

I’ve been loving Visual Studio 2012’s SQL Server Object Explorer (SSOE) instead of always jumping out to SQL Server Management Studio (SSMS) to do database management. SSOE covers many (not all) common tasks…and many more than VS’s Server Explorer let’s you do.

But I spent an embarrassing amount of time trying to figure out how to easily connect an MDF file to my SQL Server 2012 LocalDB instance because, duh …I was doing it wrong! Over and over and over again.

SSOE lets you inspect databases that are already attached to localdb and it lets you create new localdb databases. I couldn’t figure out how to attach to an existing database.

Finally, some brain cells returned to my skull and I went over to VS’s Server Explorer and attached the database there (and provided a nice logical name rather than ending up with the rangy file-path-as-database-name).

Once it was attached to localdb, then I could see it in SSOE and actually access it in my app using a normal connection string.

Just “duh” for how much time I spent on this.

It was important to me because I need to distribute sample solutions along with existing databases and I want the database files in the solution folder and *simple* steps I could relay to users to make sure they could use them when they want to debug and test out the solution themselves. I’m perennially looking for a simpler way to do this. I’ve used SQL Express, SQL CE, database scripts, packaged exes, you name it. And I always spend hours trying to do it in a way that it’s really simple for the dev who is trying out my sample solution.

Oh and one more trick for you about localdb

I have always had a problem remembering forward slash vs. back slash. Here’s a blog post from 2006 as evidence! So connecting to localdb is always a challenge because you specify it as (localdb)\v11.0.

My little secret to get the back slash correct is visual:

image

I know it’s totally dorky, but I just make sure that the slash is parallel to the first part of the “v”.  Hey, it works for me, what can I say? 🙂

April Conferences Where I’ll be Teaching, Plus a Workshop

I’ve enjoyed staying home in Vermont for most of 2012 but the wanderlust is setting in. I’m scheduled to speak at two conferences in April:

DevIntersection April 8-12, Las Vegas

I’ll be doing 3 talks at this conference, but am not exactly sure which ones will be selected yet. And yes, there will be some entity framework involved. 🙂 This is going to be a great conference and I’m excited to be included in a great lineup of speakers!

Database Days, April 17-19 in Baden Switzerland

*Full Day Workshop April 17th : Hard Core Entity Framework for Enterprise Developers
During the conference proper, I’ll do two talks:
*One for developers: Entity Framework Code First Migrations
*One for DBAs: Entity Framework & Your Database: Not as Evil as You May Think

While at Database Days, I’m looking forward to seeing Baden, a beautiful and historic town.

My new rig (aka a development box for a geek who is not a hardware nerd)

This fall I got myself a new development computer. I did my usual indecisive Libra thing over for months before settling on something.

My last machine was one that I got arm-twisted into building myself. I’m just not made for this. It should have been obvious when the parts list I selected from Newegg did not include a processor. When the parts arrived they sat in their boxes for 6 weeks. When I finally put it together it took me over 8 hours. And it never worked right. Ever. Even though I spent a lot of time over the next two years trying to figure out and fix it’s problems.

For this NEW machine, I gave it the old college try, having learned from my earlier experience and came up with a parts list that someone kindly pointed out had a motherboard and a case that were incompatible. I made many lists over the course of 4 weeks.

Finally another geek friend told me what I wanted to hear …just get a Dell and be done with it. That’s what I have normally done. This time I did buy it with an advance plan to customize it a bit. So here’s what I did.

I started with a Dell Optiplex 9010. It is a good workhorse for development.

That has an eco-friendly option of a low powered power supply which I was happy to select. I also went for the mini-tower which gave me enough flexibility to add in an extra drive.

The specs I chose were:

  • Processor: 3rd Gen Intel Core i7-3770 Processor (8MB, 3.4GHz)
  • Memory: 4GB
  • Graphics Card : (Proprietary) 1GB AMD RADEON HD 7570 without Adapters (supports 1 DVI and 1 Display Port)
  • Drive: 1TB SATA 
  • O/S: Windows 7 Professional,No Media, 64-bit
  • DVD: 16X DVD+/-RW SATA, Data Only, OptiPlex 901
  • No mouse, no keyboard, no monitor

Thanks to whatever deals were available at the time, this ended up costing $1050 plus tax and free shipping.

At the same time I ordered from elsewhere:

  • Crucial C4 256 SSD drive (2.5” with adapter)
  • Western Digital 2 TB Green Drive (not quite as fast as their Velociraptor but uses less energy :)) 3.5”
  • 12 GB RAM from Crucial

I also ended up needing:

  • a DVI to Display Port adapter for my 2nd monitor (I ended up with this)
  • an extra hard drive tray – proprietary to Dell and specifically made for that model. I should have ordered it with the computer! (for an extra $9 rather than $25 from dell after the fact or trolling ebay)

Putting it all together:

  • I took out the 1TB drive and it’s still on my shelf but will go to good use.
  • I installed the SSD as my O/S drive and the 2TB drive as a secondary drive.
  • I installed WIndows 8 pro on the ssd and split it in half. So 128 for the c: and 128 for d: which will be critical data that I use a lot. The rest of my stuff is on the 2 TB drive.

Note that this is not where I store things like photos and videos. This is just my development machine. I also have a laptop for email, writing, etc and a have an in house NAS that I use for storage and sharing.

  • I also installed the extra RAM so I have 16 GB.
  • I have two DVI monitors plugged into this machine. One is plugged into the DVI port on the graphics card and the other into the display port on the same graphics card (via the DVI to DP adapter I bought).

The performance is great (if you ignore the graphics which are more than sufficient for my needs).

image

And it’s very quiet which is important because I record screencasts for Pluralsight.com. So quiet in fact that I started getting bothered by the clock ticking on the wall and had to move it!

Where I notice the difference in speed on this machine is when I’m rendering videos in Camtasia. I imagine some of this may be due to the fact that it’s also a new version of Camtasia, but the videos render SO fast. What used to take about 45 minutes now takes about 2 minutes. I’m sure that the RAM, the processor and the SSD all contribute to that.

So I’m happy with this and have been using it for a few months now and just wanted to share in case anyone is in my boat — which is that I am NOT a hardware nerd and don’t have it in me to become one. And it did not cost me an arm and a leg. All told it was under $1500 which is relatively inexpensive for what I ended up with. Also, I need to consider that this is one of the few monetary investments I have to make for my business once every few years which makes it a bargain!

Sweating over all of the options will be the death of me some day. Thanks to the super-sensible Ryan Christensen-Schwarz (@mamanze) for talking sense into me. He was the one who said “oh just get a Dell”.

Beware machine.config files messing up Code First provider factories

It took us a long time and a bit of luck to get to the bottom of this problem. It turns out that at two completely different client sites they had computers whose machine.config file had been affected in a way that was causing an error in Code First.

The error that was being thrown at runtime was:

“Unable to determine the provider name for connection of type ‘System.Data.SqlClient.SqlConnection’”

Yet the database was reachable from ADO.NET code in the app that used the same connection string.

This led us down a lot of exploration of the provider settings in the web.config file.

Finally a clever developer at the client site wrote a little debugging tool that helped us see the *true* error which helped him get to the bottom of the problem.

The real error was:

"’DbProviderFactories’ section can only appear once per config file error’"

And it turned out that the machine.config had extra DbProviderFactories sections in it’s system.data element.

We are still trying to figure out what may have created this problem in machine.config but in the meantime, I’m trying to create some searchable text to help the next person who hits this problem.

I’ve ensured that the EF team is already aware of the problem.

Here is the MSDN forum thread related to this problem along with the solution:

Unable to determine the provider name for connection of type ‘System.Data.SqlClient.SqlConnection’.

Digging in to Multi-Tenant Migrations with EF6 Alpha

I wanted to give the new Multi-Tenant migration feature in the EF6 alpha (released Oct 30, 2012) a whirl and having done so, thought I would save someone a few steps figuring it out. The specs are very useful and thankfully I’m fairly familiar with migrations already so I didn’t have much difficulty getting it to work.

First, Some Caveats and Explanations

I’d like to start with a few caveats though. And for those of you who aren’t familiar with multi-tenant databases, a very brief explanation.

A multi-tenant database is able to isolate different groups of tables that are to be used for different purposes – most typically different applications. One method of doing this is commonly done with schemas e.g.: [JuliesApp].Customers, [JuliesApp].Addresses, [JuliesApp].Employees, [FransApp].SoftwareProducts, [FransApp].SoftwareVersions, [FransApp].Customers. (Note that those are [Schema].TableName, not just table names with a period in the middle.) I’ve got clients with databases that use multi-tenancy, so I am very interested in this feature.

Adding in Ido’s explanation from the comments(since there are multiple ways people use MT databases)

"hosting multiple clients on the same database-application server pair thus reduce overhead of deployment, maintenance, cost and some others."

Here’s an article on MSDN that’s much more authoritative on the topic of what & why than I can ever be.  Multi-Tenant Data Architecture

Up through EF5, Code First lets you specify a schema for each entity mapping using the ToTable/[Table()] configuration where you could add a schema name along with the table name. This required explicitly setting the table each time. Now with EF6, you can specify a schema per model in the OnModelCreate override with ModelBuilder.HasDefaultSchema(“People”). So if you are defining your models to represent a multi-tenant database, it is now simpler to specify the schema for all of the tables that are mapped to by your model’s entities.

Here is where I want to make first caveat. I’ve been talking and writing a lot lately about DDD Bounded Contexts and having multiple models in your domain. With my Bounded Context models, there can be a lot of overlap in mappings to the database. This is not where I would use HasDefaultSchema. With the pattern for creating multiple models for Bounded Contexts in a single app, I have also suggested a single context to be used for all database initialization. I’m not going to go into more detail on that but I just wanted to be sure that those of you who have seen my Pluralsight course, Entity Framework in the Enterprise, or my January 2013 MSDN Data Points column — if you are reading this after that’s been published — not to mistake my mention of “multiple models for multi-tenancy” for “multiple models for Bounded Contexts”.

Once you’ve defined various schemas, in EF5 and earlier, Code First migrations does not support this multi-tenancy. That’s changed in EF6. The team has added in the ability to create and execute migrations per model. And in order to do so they’ve changed some of the workflow for the metadata history tracked by the database. This feature is already available in the early EF6 alpha that was released at the end of October. You can read the specs on the EF CodePlex site.

 

rowan_mtd

 

Here is the second caveat. If you have seen Rowan Miller’s blog post about multi-tenancy with EF 4.1 Code First, that is focused on a particular scenario—repeating the same model in a database with different schemas. Here’s a screenshot from his blog post that explains what I mean:

I actually spent quite a lot of time with the new migrations feature to try to follow this path and since it’s an edge case, it’s not (yet?) readily supported. I eventually found a way to do it but it was pretty convoluted and I won’t bother sharing it here.

 

 

Trying out the Feature: First we need an M-T Database

 

I’m starting with two simple solutions where I’ve already defined my classes, model and a little console app to exercise them. They will both use the same database.

 image   image

The Models:

public class HotelRoomsModel : DbContext
{
  public DbSet<Hotel> Hotels { get; set; }
  public DbSet<Room> Rooms { get; set; }
  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    modelBuilder.HasDefaultSchema("Hotel");
    base.OnModelCreating(modelBuilder);
  }
}

 public class CasinoSlotsModel : DbContext
 {
   public DbSet<Casino> Casinos { get; set; }
   public DbSet<SlotMachine> SlotMachines { get; set; }
   public DbSet<PokerTable> PokerTables { get; set; }
   protected override void OnModelCreating(DbModelBuilder modelBuilder)
   {
     modelBuilder.HasDefaultSchema("Casino");
     base.OnModelCreating(modelBuilder);
   }
 }

Now I’ll initialize the model in the Casino app with some simple code:

  using (var context = new CasinoSlotsModel())
  {
    context.Database.Initialize(force: true);
  }

And my database gets created.

image

 

If I initialize the second model using similar code in the HotelApp, I get a second database: DataLayer.HotelRoomsModel. Not what I wanted. That’s because I’m using defaults.

Point Both Models to the Same Database

I need to use one of the many ways to direct both models to the same database. I’ll keep it simple for now and do that in the constructor for both model classes.

 public class HotelRoomsModel : DbContext
 {
   public HotelRoomsModel(): base("CasinoHotels")
   {      }

 public class CasinoSlotsModel : DbContext
 {
   public CasinoSlotsModel():base("CasinoHotels")
   {    }

 

Now I’ll initialize both models again.

image

Just what I wanted! Notice that I am using the default initialization still which for Code First is CreateDatabaseIfNotExists. I have not switched to migrations yet. So when it initialized the first model, no problem..there was no database yet so it created it. When it initialized the second model, the database already existed, but EF6 understood what to do since it was a separate tenant in the database.

If I had a type called Room in my Casino app, I’d also have a Casino.Rooms table in the database. The two apps know which tables are theirs so it doesn’t pose a problem.

Notice also that under System.Tables, I have some new tables that Code First uses to track the metadata from both app – or really for both schemas:

image

This is an important change in EF6.

 

Modifying something in the model and Initializing again – A Surprise

There are many ways to modify a model. You can add another DbSet to the model. You can change one of the classes that the model exposes. You can modify how the classes map to the database. I’ll do a quick mod – add a property to the Room class in the Hotel app. If I run the intialization again, EF should see that the model is already represented in the database, but in it’s former definition. Because I’ve changed the model, I anticipate that EF will suggest that I use migrations along with the big fat exception it will throw. (This is just how it’s been since EF 4.3.) But it didn’t! It just initalized happily without reporting a problem and the new property is completely ignored by Code First. That will end up with a runtime surprise down the road when I try to query or update using the new property.

I did some experiments to verify that the HasDefaultSchema is responsible for this. I should probably look to see if this is logged in Issues yet. 🙂

Update 11/19: Andrew Peters from the EF team confirmed the problem and has added this work item to correct it.

Enabling Migrations for *both* tenant apps

I’ll just skip ahead and switch to migrations anyway. If you’ve used migrations this will be familiar; but there are a few twists.

I have two projects with their own models in the, so I need to enable migrations in both projects.

In the package manager console window of each solution, I’ll enable migrations for the project with models in them:

PM>enable-migrations -ProjectName:DataLayer.CasinoModel -MigrationsDirectory:CasinoMigrations 
PM>enable-migrations -ProjectName:DataLayer.HotelModel -MigrationsDirectory:HotelMigrations 

Since I don’t have multiple models in a single project (I recommend against doing that anyway), I don’t believe that I really need to use the new MigrationsDirectory parameter. But I’m trying out the new features so in it goes! What this does is name the folder with the name I provided (e.g. CasinoMigrationsl or HotelMigrations) instead of just “Migrations”.

image       image 

It also adds the MigrationsDirectory setting in the Configuration class. You can read more about MigrationsDirectory in the specs.

    public Configuration()
    {
      AutomaticMigrationsEnabled = false;
      MigrationsDirectory = @"CasinoMigrations";
    }

A Configuration Kludge that May only be needed because of a Bug in the Alpha Bits

When the CreateDatabaseIfNotExists default initializer created the database, it created one MigrationHistory table for each model I was tracking. Those tables contain a [new to EF6] field called ContextKey. By default, Code First uses the strongly-typed name of the context for that ContextKey value in each row added to the table. When I switched to Migrations, it was looking for a context key of the schema name (that seems to be expected behavior). However, my interpretation of the specs was that Code First would be able to deal with this change, but it didn’t seem to be. Not finding a MigrationHistory table that contained any rows with “Hotel” as the context key, Migrations tried to create a new Hotel._MigrationHistory table. That failed because it already existed. My workaround (for now) was to explicitly set the context key to use the old default.

    public Configuration()
    {
      AutomaticMigrationsEnabled = false;
      MigrationsDirectory = @"HotelMigrations";
      ContextKey = "DataLayer.HotelRoomsModel";
    }

I did the same for the Casino Migration Configuration file setting ContextKey to “DataLayer.CasinoSlotsModel”.

I don’t know if I misunderstood what I read in the specs or if there’s a bug. So I’ll keep an eye on that one with further releases.

Update 11/19: Andrew Peters from the EF team has confirmed this problem and created this work item to fix it.

Use Code-Based Migrations, not Automatic

I set AutomaticMigrationsEnabled=true (and told Code First I was using the Migrations Initializer) for the HotelRoomsModel to see what would happen as a result of adding a new property to the Room class. I got the following error message when I tried to initialize the context for that model.

 image

So you need to explicitly create and execute migrations for each model as needed in the Package Manager Console Window.

Let’s see how that goes. Remember I’ve added a new property to the Room class since I initialized the database.

PM> add-migration NewPropertyInRoomType -ProjectName:DataLayer.HotelModel 

This creates a new migration named “NewPropertyInRoomType” for my HotelModel project.

image

And the migration recognized the correct change to the model:

  public partial class NewPropertyInRoomType : DbMigration
  {
    public override void Up()
    {
      AddColumn("Hotel.Rooms", "FirstNewPropertyWithMigrations", c => c.String());
    }
        
    public override void Down()
    {
      DropColumn("Hotel.Rooms", "FirstNewPropertyWithMigrations");
    }
  }

Now I have to explicitly execute this using update-database but making sure again that the command is executed for my HotelModel project. (FWIW, the PackageManager Console windows has a drop down to select the appropriate project to execute each command in but I’ve found that I prefer to just do that in code rather than in the UI.)

PM> update-database -ProjectName:DataLayer.HotelModel  -verbose

I like to use –verbose so I can see what’s happening. 🙂

I’ve got the new property in my database table now.

image

Additionally, I can see that the migration and representation of the current model were added as a new row into the correct MigrationHistory table: Hotel.__MigrationHistory.

image

I’m satisfied for now. I actually spent a long time on this because I hit walls, had to back up, explore different behavior, go back and read the specs repeatedly, etc. But hey, a girl’s gotta have some fun, right? 🙂 (yes this is indeed fun for me…when I have a little free time on my hands. 🙂 )

Check it Out Yourself and Give Feedback to the Team

So if you are interested in multi-tenant database support, please check out the feature and provide feedback either in the Issues or the Specs page for this feature. This is an early alpha and you can help shape how it works when EF6 releases.

*Thanks to Frans Bouma for pointing out that I hadn’t clearly divided this into two app on my first pass. I took the post off-line for about 1/2 hour to make the revision.