Archive for the ‘Entity Framework’ Category

WARNING: EF Code first may only be appropriate for children

I would like to warn anybody who wants to use code first (specifically TPC inheritance) to only use it when you plan to stick with the simple scenarios that you’ve already seen proven out through examples on the web.  Once you begin to stray from these examples, you’ll find yourself with code that doesn’t work with no way to fix it (since, per the Microsoft way, the important classes are all “private internal”).  Here are just a few examples of limitations that I’ve encountered:

1) If you want an entity base class that is not abstract (to, say, work around a bug in RIA services) with a TPC (Table per Concrete Type) configuration, forget about it… there is absolutely no way to do it.

2) Generic types are not supported as entities or as entity base types.

3) If you want to define the properties on your entity at runtime, using ICustomTypeProvider or IDynamicMetaObjectProvider… again, forget about it.

I could go on and on, but the point is that I’ve run into more “not supported” scenarios with this library than almost any other I’ve ever used.  That said, I love the concept… and once EF code first becomes at all extensible, I’ll fully support it.

Entity Framework Code First Migrations: Executing migrations using code, not PowerShell commands

Migrations are cool, but all the documentation only shows how to automatically update the database using the Visual Studio Package Mananager Console, like so:

Update-Database

However, I want my database to be updated through code like some of the older builds of Entity Framework Code First allowed.  Well, I did some digging, and here’s what I came up with.

var migratorConfig = new DbMigrationsConfiguration<NorthwindContext>();
migratorConfig.AutomaticMigrationsEnabled = true;

var dbMigrator = new DbMigrator(migratorConfig);
dbMigrator.Update();

If you’ve enabled migrations for your project using the Package Manager Console, you should have a file called Configuration.cs in your Migrations folder.  You can use this instead of using DbMigrationsConfiguration<>.  So with this setup, you’d have:

var migratorConfig = new ProjectNamespace.Migrations.Configuration();

var dbMigrator = new DbMigrator(migratorConfig);
dbMigrator.Update();

Lastly, you should be aware that the DbMigrator constror creates an instance of your DbContext.  So it’s very easy to create an infinite loop if you try to update the database from your DbContext contructor or from an implementation of IDbContextFactory.Create().  However, this is exactly what I wanted to do, so I just had to add some thread-safe checks around my database migration code:

public static int IsMigrating = 0;
private static void UpdateDatabase()
{
    if (0 == Interlocked.Exchange(ref IsMigrating, 1))
    {
        // Manually creating configuration:
        //var migratorConfig = new DbMigrationsConfiguration<NorthwindContext>();
        //migratorConfig.AutomaticMigrationsEnabled = true;

        // Using configuration defined in project:
        var migratorConfig = new ProjectNamespace.Migrations.Configuration();

        // 3
        //var dbMigrator = new DbMigrator(new Settings());
        var dbMigrator = new DbMigrator(migratorConfig);
        dbMigrator.Update();

        Interlocked.Exchange(ref IsMigrating, 0);
    }
}

Code first and WCF RIA services: Failed to get the Metadataworkspace for the DbContext type ‘{Type}’

While working with WCF RIA services, DbContext (DomainContext), Entity Framework code first, and Silverlight, I received this error quite a bit whenever I tried to run or compile the Silverlight project. 

The error means that there is something wrong with your code first model.  For me, it was usually some attribute that had a wrong string it.

It is annoying because it’s almost impossible to fix this error without knowing the tricks. Good news though!  The way to get around this error is to browse to your WCF RIA service. 

1) In order to make your service browsable at all (at least consistently), you should make your project use IIS rather than IIS Express.  Go into the project settings, under the web tab, and make sure “Use Local IIS Web server” is selected and “Use IIS Express” is not selected.

2) Now you need to find the URL your domain service uses. 

2.a) Make sure “Show all files” is enabled at the top of your Solution Explorer window. 

2.b) In your Silverlight project, expand the folder “Generated_Code”.  Open the file at the root of that folder.  Search for “http://”.  You’ll find a URL that looks similar to:

http://localhost/{WcfRiaServiceProjectName}/{Domain-Service-Namespace-And-Type-Name}.svc

3) Compile the web project by itself so that you have the most up to date error showing when you browse to the URL.

4) Browse to the URL!  It should give a much better error message.  Make sure you have the web project showing exception details. If you don’t, the web page will tell you how to do so.