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
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:
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With