Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which project should I enable migrations

I have a wpf project, with the structure below:

project1(solution)
|->Model(project)
|->DataAccess(project)
|->project1(project)

Project1 is the project where I compile and deliver the exe to the user.

Now I want to enable automatic migration: Enable-Migrations –EnableAutomaticMigrations

project1 is the default project. When I run the command, obviously it won't be able to find the database context, which is located in the DataAccess project. I am able to enable it in the DataAccess project.

But is it correct? How can I enable it in the project1 project?


EDITED:

I think I can enable migration in the DataAccess project, and have it MyConfiguration reference back to DataAccess project?

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, MyConfiguration>());

But I get this error:

Error   2   'DataAccess.Migrations.Configuration' is inaccessible due to its protection level
like image 992
Kev Fixes Avatar asked Nov 06 '12 05:11

Kev Fixes


1 Answers

Have you tried using the -StartupProjectName parameter?

Enable-Migrations -EnableAutomaticMigrations -ProjectName DataAccess -StartupProjectName project1

The last parameter specifies to emulate running within that project, including any settings from app.config/web.config.

Edit: as for enabling migrations, the error you're seeing is probably because the MigrationsConfiguration is created by default as internal. You can either:

  • Make it public
  • Set the DB initializer somewhere in the DataAccess project
  • Set it in the app.config/web.config for project1. Even though the type is internal, it will resolve at runtime and still work:

    <entityFramework>
      <contexts>
        <context type="DataAccess.FooContext, DataAccess">
          <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[DataAccess.FooContext, DataAccess], [DataAccess.Migrations.Configuration, DataAccess]], EntityFramework, PublicKeyToken=b77a5c561934e089">
          </databaseInitializer>
        </context>
      </contexts>
    </entityFramework>
    
like image 184
Jimmy Avatar answered Oct 07 '22 00:10

Jimmy