Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dnx ef database update not modifying database #4188

Closed
ealsur opened this issue Dec 29, 2015 · 12 comments
Closed

dnx ef database update not modifying database #4188

ealsur opened this issue Dec 29, 2015 · 12 comments

Comments

@ealsur
Copy link

ealsur commented Dec 29, 2015

We are using EntityFramework.MicrosoftSqlServer version 7.0.0-rc1-final. Running on dnx 1.0.0-rc1-final (Windows).

We followed the procedure described on https://ef.readthedocs.org/en/latest/getting-started/aspnet5/new-db.html
When we run dnx ef migrations add we can see the files being created in the Migrations folder. Upon inspecting them, we can see that it's indeed creating our tables for our database (Azure SQL DB).

But if I run dnx ef migrations list we see no migrations listed.

capture

So when we run dnx ef database update we only get a Done message but the database contains none of our tables and only an empty __EFMigrationsHistory.

Tried it with a local db on a SQL Express but the same thing happened.

Our DBContext is on a Class Library project being referenced but all the namespacing of the generated migration files seem ok.

Any way this can be debugged?

@ealsur
Copy link
Author

ealsur commented Dec 29, 2015

Ok, I cloned the EF repo and found out that the problem was on https://github.com/aspnet/EntityFramework/blob/dev/src/EntityFramework.Relational/Migrations/Internal/MigrationsAssembly.cs#L33

It's reading the Assembly from the Context (which in my case is another referenced project) but the Migrations file was created running the command on the project that actually uses the Context (the one with the Startup.cs file).

On that same line, it's trying to parse some Option that might define the Assembly, but I found no commandline options for that on the dnx ef database update.

@ealsur
Copy link
Author

ealsur commented Dec 29, 2015

As a workaround, I copied all the Migration files created on my Api project to the Context project and adjusted the namespace on them.

The dnx ef database update command worked perfectly afterwards.

@bricelam
Copy link
Contributor

This experience may have been improved by #4098

@ealsur
Copy link
Author

ealsur commented Dec 29, 2015

@bricelam awesome :)
I found #3879 to be almost exactly what I went through but from the other side, I ran the ef command on the Web/Api (because I was following all the examples) and they ran it on the DbContext project (and added a Startup.cs bootstrap).

In this scenario, the Migration files should be inside the DbContext project, but that leads to a DbContext project needing Mvc dependencies to have a Startup.cs file, which doesn't sound too "clean".

From your reference I understand that I'd get a Throw if I ran the migration command on the Web/Api project once that gets merged and part of a released version, is that correct?

@bricelam
Copy link
Contributor

The root problem was that you could add migrations to a project that wasn't configured as your migrations project. Then when you try to apply those migrations they can't be found. This stops you from doing that and instructs you either to add them to a different project or configure you migrations project to be the one you're trying to add them to.

@bricelam
Copy link
Contributor

Ultimately, your Startup, DbContext & Migrations classes can each be in their own project. You just need to configure the migrations assembly, invoke the command on the startup project specifying the target project.

@markusvt
Copy link

markusvt commented Jan 4, 2016

I ran into the same Problem. How can I configure the migrations assembly?

Edit: I only managed to specify the target project for the add command with
dnx ef migrations add Initial -p Database

If I then run the list command, I see the migration.
But the target project specification does not seem to work with the list command, it seems it always reads the project containing the DBContext

@dougludlow
Copy link

I've been beating my head against the keyboard for the past couple hours over this same problem and then finally came across this issue. I got it working thanks @bricelam's last comment.

So given two separate projects, say a web project and a class library, if you have a separate context in each an you run commands from the web project (the one with the Startup.cs)...

dnx ef migrations add ClassLibraryMigration -c ClassLibraryContext -p ClassLibrary

Will put the migration in the ClassLibrary project.

dnx ef database update -c ClassLibaryContext

Will apply the migration to the database.

@ealsur
Copy link
Author

ealsur commented Jan 5, 2016

Awesome @dougludlow I was trying to achieve it but couldn't find the correct dnx ef migrations add syntax.

I'll close this issue now.

@ealsur ealsur closed this as completed Jan 5, 2016
@jmevel
Copy link

jmevel commented Feb 26, 2016

@dougludlow I've been looking for this for hours now. Thank you so much!

@dougludlow
Copy link

👍

@ishak91
Copy link

ishak91 commented May 14, 2016

Hi, My Migration class are not creating in my class library project. all the migrations codes are moving to wrap folder
../WebApplication2\ wrap \ClassLibrary1\Migrations

--EF Command Line Output

`F:\Developemnt\ASP.NET 5\WebApplication2\src\WebApplication2>dnx ef migrations add Init -p ClassLibrary1 -c DBContext -v

Finding DbContext classes... 

Using context 'DBContext'.

Writing migration to 'F:\Developemnt\ASP.NET 5\WebApplication2\wrap\ClassLibrary1\Migrations\20160514092805_Init.cs'. 

Writing model snapshot to 'F:\Developemnt\ASP.NET 5\WebApplication2\wrap\ClassLibrary1\Migrations\DBContextModelSnapshot.cs'.
Done. To undo this action, use 'ef migrations remove'`

F:\Developemnt\ASP.NET 5\WebApplication2\src\WebApplication2>dnx ef database update -c DBContext -p ClassLibrary1 -v
Finding DbContext classes...
Using context 'DBContext'.
Using database 'SchoolDB' on server 'AHAMEDISHAK-D'.
Done.

So dnx ef database update -c DBContext -p ClassLibrary1 -v is still not working.

Ones I copied the Migration Folder from wrap folder to ClassLibrary Project then it works fine

What's i'm doing wrong ?

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants