I'm starting to turn migration manuals in my solution.
But I have a problem, it is several days that I could not understand.
My solution is divided into several projects, a project for the view (Sedna.UI) a project for the db where are my entity and my contexts. (Sedna.Domain).
I have 3 contexts, 2 have automatic migrations while the main one I would turn migration manual (SednaContext)
Nuget package manager console there is a dropdown and I select the Default project "Sedna.Domain" then when i run Enable-Migrations -ContextTypeName Sedna.Domain.UnitOfWork.SednaContext ... I get an error: "No migrations configuration type 'Sedna .Domain.Migrations.Configuration 'was found in the assembly "Sedna.Domain" "while I find him in the project.
I'm going crazy I do not understand why?
Screencast
Try specifying the ContextProjectName as well. http://bartwullems.blogspot.com/2014/05/entity-framework-migrations-no-context.html Also see Enable Migrations with Context in Separate Assembly?
Related
In a standalone entity framework 7 project (note, not a MVC project with an entity context where the command DNX might be used), how can I run a migration on a remote database using Powershell?
I am currently using Entity Framework 7.0.0-rc1-final with Visual Studio 2015 (14.0.23107.0).
At the moment there is no way you can use pure PowerShell to do this because a utility like migrate.exe does not exist yet and importing the EF PS modules is not possible as they require a Package Manager PowerShell Host.
Here are some ideas how you can update a remote db in EF7:
One thing you could do is use the package manager console commands from within VS as usual to update the remote db. You can create a second context that has the remote db connection string and use the update-database command specifying the context to use. These commands require the following package in EF7:
https://www.nuget.org/packages/EntityFramework.Commands/.
I have done this successfully in a class lib project.
Another solution would be to use DNX commands by creating a DNX project instead of a classic one. DNX projects are not just for web sites, it is just another type of project. Here is a link that shows how to create a console app DNX project:
http://docs.asp.net/en/latest/dnx/console.html.
So with this type of project you can use the provided DNX commands that you seem to be aware of.
I hope this helped. Maybe we can give more help if you describe your situation and your end goal in more detail.
Answer too long as a comment, so adding it here...
Have you looked at this article and the links in the answer?
From that answer
The problem with importing the module into a PowerShell console is that I believe the module expects to run in a context where it has a Visual Studio DTE object available. That environment is the NuGet Package Manager Console. This issue has been brought up before. Check out this blog post and this SO question.
This blog post shows how to write code that does migrations.
What might be helpful for readers of this question is what you have tried, what is not working, and other information that might help solve your problem.
I am trying to use EF7 with the old project type (csproj)
If I run Add-Migration in package manager, it complains that it cannot find package EntityFramework. I guess it is looking for the EF6 EntityFramework project, since such a project does not exist anymore? How can I force it to use EF7 Add-Migration cmdlet?
I have tried running ef commands with dnx in the "wrap" folder, but that seems needlessly complicated and it doesn't work (it cannot find any framework dependencies at all)
Any ideas?
Have you installed EntityFramework.Commands? https://www.nuget.org/packages/entityframework.commands
Also, using EF6 and EF7 in the same solution is going to cause some problems. It's probable that Add-Migration failed because you are actually running the EF6 command. Check if the command Use-DbContext exists, which is new to EF7.
PM> enable-migrations
I get an error(eManager) like this.
I have two projects in my VS solution named as below :
eManager.Domain
eManager.Web
I added reference of eManager.Domain to eManager.Web even though its popping out the same error everytime.
error here :
More than one context type was found in the assembly 'eManager.Web'.
To enable migrations for eManager.Web.Infrastructure.DepartmentDb, use Enable-Migrations -ContextTypeName eManager.Web.Infrastructure.DepartmentDb.
To enable migrations for eManager.Web.Models.UsersContext, use Enable-Migrations -ContextTypeName eManager.Web.Models.UsersContext.
Use this article and this link
The problem is easy to solve. As the error states, you can use the –ContextTypeName flag to specify your context class name. Note that you can only have migrations for one context in a project, so if you want to have migrations for both contexts you’ll need to move one to a different project. Again, my recommendation is to just remove the existing UsersContext the Internet project template creates, and take ownership of the user profile in your own context.
In the Package Manager Console you have to change the Default Projetc Drop to your Web project
and after exec enable-migrations for the context you want like that:
PM> Enable-Migrations -ContextTypeName Web.Infraestruture.DepartmentDb
But remember you already have set the web as Startproject.
I have created a module to be included in a main MVC3 web application. The module is packaged into a NuGet package and it can be installed and uninstalled via NuGet. Both, the main site and the module use Code First, EF >= 4.3
To create the package I have another MVC3 site and all the functionality is inside an area, so to create the package I just pack the libraries, the views and all the needed files. Database Migrations work fine in the project and the package is created nicely.
Now I install the package in the main site via NuGet. This site is in another solution, and the solution has two projects:
MyProject.Web.UI: this is an Mvc3 project
MyProject.EntityFramework: this is a class library with all the models, dbContext for MyProject...
The package is installed correctly and the Area, the Area views and libraries are correctly installed.
The problem now is how I update the database? I've tried first to run "Update-Database" but I get the message:
"No migrations configuration type was found in the assembly
'MyProject.Web.UI'. (In Visual Studio you can use the
Enable-Migrations command from Package Manager Console to add a
migrations configuration)."
I've tried then to enable the migrations with "Enable-Migrations" but I got this other message:
"No context type was found in the assembly 'MyProject.Web.UI'."
I tried also just to run the site and see if the changes are automatically applied but I get the exception page with the typical message:
"The model backing the 'NugetPackageDbContext' context has changed
since the database was created. Consider using Code First Migrations
to update the database"
I don't know what to do to update the database with the required changes in migrations that come in the NuGet package. Any one could put some light here in this matter? I'm quite new to Migrations, maybe there are some configs to update the database if there is a change instead of running the commands in the console, I'm a bit lost.
Thanks in advance :)
Good news! It seems that I got it. I was looking for a way to make the NuGet package to update the database to the latest version.
Well, this package comes with an Admin controller, so I added a new action called Update:
public ActionResult Update()
{
System.Data.Entity.Database.SetInitializer(new System.Data.Entity.MigrateDatabaseToLatestVersion<MyPackageDbContext, MyPackage.Migrations.Configuration>());
return View();
}
In my Configuration class for the migrations I have:
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
I have to say that in my way to do all of this I've found a few strange behaviors. One thing that surprises me is this, I don't know if this is normal, read the secuence:
Package installed with new migrations but database not up to date. So if I access the EF this affected by this I get the exception about this. Ok up to this.
I go to my action /MyPackage/Admin/Update and run it. Apparently it runs. I go to the database and I don't see changes. Even the migrations table does not have a new row.
I access again the EF part that displayed the exception before (point number 1) and then everything goes through, database is updated and the migrations table shows the new line.
One thing that you have to notice is that the Configuration class is internal, but because this is a module I needed to be accessible from another assembly. I tried to make it public but I got some strange warnings/errors that I don't know if they are related. So in the end I kept it internal but used
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("OtherAssembly")]
I've found a few of strange behaviors in Visual Studio with all this stuff of the NuGet packages, CF, migrations ... I don't know if these things are bugs, but all this thing took me two full working days.
I hope it is useful for any other that wants to create a CF NuGet package that is updateable.
In package manager console you will need to the Default project to MyProject.EntityFramework
You may also need to make sure MyProject.Web.UI is set as the start up project (in case there are multiple), then you can pass in the connection string into update command:
Update-Database -ConnectionStringName MyConnStringName
This should update the database correctly, unless there is data that will be lost.
If your DbContext is in MyProject.EntityFramework then the Default Project in the Package Manager Console needs to be set to MyProject.EntityFramework before you can use Update-Database.
I realize that this question is very old, but since I don't see this answer I'll throw it in anyway.
To perform migrations and such on projects or external references you can still use the same three command:
Enable-Migrations
Add-Migration
Update-Database
but you wil need to supply some additional parameters. For the Enable-Migrations command you will need to add the -ContextTypeName and optionally the -ContextAssemblyName commands like so:
Enable-Migrations -ContextTypeName MyProject.EntityFramework.NugetPackageDbContext -ContextAssemblyName MyProject
This will give you a migration configuration class in your current project. The other two commands will require you to specify this configuration class:
Update-Database -ConfigurationTypeName MyProject.Web.UI.Migrations.Configuration
Hope that helps
I have looked around stackoverflow and search the internet but did not find a answer.
If i try to Enable-Migrations for my project i get the Exception
Error while searching for context type (specify -Verbose to see exception details).
Edit the generated Configuration class to specify the context to enable migrations for.
So my question is:
What are the requirements for my project / DbContext in order to let the Entity Framework Migrations find my DbContext WITHOUT specifying it manually ?
Thanks in advance!
You project will need a database class which derive from DbContext, and in your web.config/app.config, you will need a connection string which points to the right database which is used by your database class.
The project you run the command on will need a configuration file to have the right connection string. Even it's class library, you still need this configuration.
I have had a similar issue which was related to the wrong version of Entity Framework being referenced.
You can try executing Update-Package EntityFramework from the Package Manager Console in Visual Studio.
If that does not help then just manually reference the EF 4.3 library from your project and then re-run the Enable-Migrations -force for your project(s).
Cheers.