7

I have only 2 days experience with EF Migrations, so please be kind...

I have a fairly large existing WPF WCF MVVM EF 4.1 solution which needs to be migrated to EF 4.3.1 and begin using Migrations. The solution's "Services" project contains four DbContext's, each in its own namespace, and each associated with its own database.

Before I began modifying the large solution, I did some experimentation with a small sample console app with only a single project and two DbContexts, mostly based on the example provided by "e10" (EF 4.3 Auto-Migrations with multiple DbContexts in one database). The sample app works well, and I can do add-migration and update-database separately for the two contexts (by specifying the -configuration parameter).

But when I tried to replicate the same approach with the "real" (large) solution - with four DbContexts - I ran into a problem: when I invoke add-migration in PMC and specify any of the four configuration names, add-migrations gets an exception saying it can't load the Services assembly.

Here's what I did with the large solution:

1) Added the EF 4.3.1 NuGet package to my Core, Services and UI projects (this last bit may be important).

2) created a Migrations folder in my Services project and manually created a Configuration.cs file containing four classes which inherit from DbMigrationsConfiguration<type>, where type is App, Catalog, PortfolioManagement or Scheduler. (code is below)

3) added a property to one of the model classes associated with the App DbContext, so there would be something to migrate

4) from the PMC, invoked add-migration:

 PM> add-migration App_AddNewProperty -config App

Note that I didn't do "Enable-Migrations" because, as e10 said in his post:

" You dont need to enable migration since you already did with the ... classes above" (referring to the classes in Configurations.cs).

5) add-migration gets exception: Could not load file or assembly 'MyApp.Services' or one of its dependencies

I enabled binding-failure logging, and the failure log shows that it's trying to locate the Services assembly in the UI's bin/debug folder, rather than in the Services project).

And it fails the same way even if I have the Default Project in the PMC set to the Services project (Default Project defaults to the UI project).

I suspect this is caused by the UI not having a reference to the Services assembly (it has a WCF Service Reference, but not an assembly reference). But if this is the problem, how do I force PMC to not start at the UI project? Or can I "unassociate the UI project from the EF package"?

Thanks!

DadCat

Configurations.cs:

namespace MyApp.Services.Migrations
{
    internal sealed class App : DbMigrationsConfiguration<Geophysical.Skimmer.Services.App.Repository.ModelContainer>
    {
        public App()
        {
            AutomaticMigrationsEnabled = false;
            MigrationsNamespace = "MyApp.Services.App.Repository.ModelContainer";
        }

        protected override void Seed(MyApp.Services.App.Repository.ModelContainer context)
        {
            ... no code here
        }
    }

    internal sealed class Catalog : DbMigrationsConfiguration<Geophysical.Skimmer.Services.Catalog.Repository.ModelContainer>
    {
        public Catalog()
        {
            AutomaticMigrationsEnabled = false;
            MigrationsNamespace = "MyApp.Catalog.Repository.ModelContainer";
        }

        protected override void Seed(MyApp.Services.Catalog.Repository.ModelContainer context)
        {
            ... no code here

        }
    }

    internal sealed class PortfolioManagement : DbMigrationsConfiguration<Geophysical.Skimmer.Services.PortfolioManagement.Repository.ModelContainer>
    {
        public PortfolioManagement()
        {
            AutomaticMigrationsEnabled = false;
            MigrationsNamespace = "MyApp.PortfolioManagement.Repository.ModelContainer";
        }

        protected override void Seed(MyApp.Services.PortfolioManagement.Repository.ModelContainer context)
        {
            ... no code here
        }
    }

    internal sealed class Scheduler : DbMigrationsConfiguration<Geophysical.Skimmer.Services.Scheduler.Repository.ModelContainer>
    {
        public Scheduler()
        {
            AutomaticMigrationsEnabled = false;
            MigrationsNamespace = "MyApp.Services.Scheduler.Repository.ModelContainer";
        }

        protected override void Seed(MyApp.Services.Scheduler.Repository.ModelContainer context)
        {
            ... no code here
        }
    }
}
4

0 回答 0