When I user the Add-Migration command of Entity Framework migration I get the following exception:
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Data.Entity.Migrations.Extensions.ProjectExtensions.GetFileName(Project project, String projectItemName)
at System.Data.Entity.Migrations.MigrationsCommands..ctor(Object project, Object startUpProject, String configurationTypeName, String connectionStringName, String connectionString, String connectionProviderName, PSCmdlet cmdlet)
Any insight?
I've seen this before when there are multiple projects in the solution and the "wrong" project is selected as the startup project. For example, somebody else reported that in an Azure hosted MVC3 website they had the Azure project as the startup project instead of the MVC project. Switching over to the MVC project as the startup fixed the issue.
Update: This has been fixed in EF5-beta2, which is now available on NuGet.
You can actually very easily specify the target project in your Package Manager Console when calling any EF command by just using the correct flags and arguments, for instance, given a project called ProjectFoo out of a solution with multiple projects:
enable-migrations -projectname projectfoo
add-migration "Initial" -projectname projectfoo
update-database -projectname projectfoo
etc...
Easy as pie.
Related
I have multiple DbContexts in a C# project and I'm trying to enable migrations. When I specify the full command, i.e.:
Enable-Migrations -ContextTypeName Models.Account.AccountDetailDbContext
A migrations folder is created, with the configuration class, but I then get a message:
Checking if the context targets an existing database...
And then
The migrations configuration type 'Portal.WebUI.Migrations.Configuration' was not be found in the assembly 'Portal.WebUI'.
Even though it has just created the file, it can't find it.
I have the correct project selected in the Package Manager Console
I have tried the command using -verbose, but it gives no additional information
If I copy the dbcontexts and classes into a new project then it all works, so it must be something in this existing project that is making the migration fail, but I can't tell what it is.
I solved this by adding EntityFrameworkCore\ before Add-Migration, i.e. the final statement was:
EntityFrameworkCore\Add-Migration
After give a name for your new migration.
I faced this problem. My solution:
Exit visual studio
Open your project again on visual studio
Rebuild solution
Then the error removed. And I can run the command.
After you run Enable-Migrations and the Configuration file is created, rebuild the project and run Enable-Migrations -Force again.
I had this problem and it was solved by changing the dropdown box at the top of the Package Manager Console to choose the correct project. You may need to maximise the width of the package manager console to see this box.
I managed to resolve this by uninstalling the EF nuget package and then reinstalling it.
I have stumbled upon this problem, and after endless hours of googling and trial and error, the solution to my specific problem was much easier.
Just make sure you have all your EF related nuget packages up to date.
I tried every answer I could find here, but what ended up working for me was: Select all EF related nuget packages
(In my case)
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
Microsoft.EntityFrameworkCore.Design
EntityFramework
And update them all, into their latest version (In my case)
The issue was that one of the defaults I downloaded of one of these NuGet packages, was outdated and invalid.
Another possible issue worth checking: is your project signed? As I just discovered, this problem can also eventuate if the assembly is signed with a strong name key file. Part of the EntityFramework tool kit is migrate.exe which is called during the migration process. It appears if the assembly is signed, this application can't find the configuration type.
Solution seems to be <Project> → Properties → Signing: untick "Sign the assembly", at least while performing migration tasks. Tick it back when you're done.
I also had this issue because of a spelling mistake in a namespace
I was facing the same issue. What I found was that in my project name, "-" was included as "abc-xyz". I deleted my project and recreated it as "abcxyz" and it worked. Don't rename the project—you have to rename it at every reference. In this case, first uninstall EF and rename it, then install EF again.
I have faced this problem due to version update of EF package. I have solved by reinstalling:
1.Microsoft.EntityFrameworkCore.SqlServer
2.Microsoft.EntityFrameworkCore.Tools
3.Microsoft.EntityFrameworkCore.Design
4.Microsoft.EntityFrameworkCore
The solution is to check the project reference. In my case, I have added Entity Framework and Entity Framework Core references to my project.
To Resolve this I have removed Entity Framework Reference from the project and now EF Core migration commands are working.
using Microsoft.EntityFrameworkCore.SqlServer;
using Microsoft.EntityFrameworkCore.Tools;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore;
Then:
Use EntityFramework6\ before Add-Migration for Entity Framework 6.
Like EntityFrameworkCore6\Add-Migration
In this case First, check NuGet packages
Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.Tools Microsoft.EntityFrameworkCore.Design
most probably missing one of the above packages you get an error.
if missing then install and
do migration using
Add-Migration InitialCreate
Using the Package Manager Console I can run a command similar to the following one
Update-Database -ProjectName: My.Project.Data
The command triggers migrations on MyDbContext defined within the project My.Project.Data located in the currently open solution. Good.
Now the project My.Project.Data resides in the same solution with project My.Project.Mvc. The problem is, My.Project.Mvc uses two db contexts. One that we already covered and another one defined within project Core.Project.Data - this one is built as a NuGet package which is then referenced by My.Project.Mvc. The diagram below should make it clear:
MySolution
├┬ My.Project.Mvc
│├─ references: My.Project.Data (in the same solution)
│└─ references: Core.Project.Data (NuGet)
└─ My.Project.Data [MyDbContext]
CoreSolution
└─ Core.Project.Data [CoreDbContext]
The question is, how can I trigger the migrations for the CoreDbContext from the Package Manager Console of MySolution. If I try to run Update-Database -ProjectName: Core.Project.Data I get the error:
Get-Project : Project 'Core.Project.Data' is not found.
Update: Using the migrate.exe instead is not an option.
From the document update-database does not support dll.
You can execute EF migration tool from commandline, or add it in VS menu for conveniences.
migrate.exe Core.Project.Data.dll /startupConfigurationFile=Core.Project.Data.dll.config
or change /startupConfigurationFile to /connectionString=your-connection-string
When migrations are first enabled by Enable-Migration, a Configuration.cs file will be added to the project. If this file exists for Core.Project.Data, tell Entity Framework to use this config:
Update-Database -ConfigurationTypeName Core.Project.Data.Migrations.Configuration -ProjectName Core.Project.Data
So basically I've two projects on the same solution. One of the projects its a class library where I have the all the Models and the Database Context class. The other one is a Web API. I want to use Nuget to Enable-Migrations on the Web API project but I always get the "No context type was found in the assembly Pr.WebApi.
So far I've tried:
Enable-Migrations -ContextTypeName Pr.ClassLibrary.Models
Any Ssuggestions?
When theres no data to store in a database in your WebAPI-Project, you dont need Entity Framework at all in this project. If you store data over your Class library, you can use the context from the class library project.
You will use Enable-Migrations in the Package Manager Console. Make sure that the default project on the top of the package manager console is set to your class library. That is the only project you need to enable the migrations on.
Enable-Migrations should be invoked in your class library...
I just got on board with EF 5 and am using their code-first migrations tool but I seem to get an error when I try to enable migrations.
I type Enable-Migrations into the package manager console and then it says
No classes deriving from DbContext found in the current project.
Edit the generated Configuration class to specify the context to enable migrations for.
Code First Migrations enabled for project MyApp.MvcUI.
It then creates a Migrations folder and a Configuration class in my MvcUI project. Thing is, my DbContext lives in a class library project called MyApp.Domain. It should be doing all that in that project and should have no problem finding my DbContext.
Oh wow, nevermind. I'm dumb.
In the Nuget package manager console there is a dropdown menu at the top labeled "Default Project:". Make sure you set that to the project you want to run the command against.
Hopefully this helps someone else avoid my embarrassing mistake.
There are actually 3 ways to make Nuget commands run in a specific project:
[Package Manager Console] Set the active project in the dropdown at the top of the console toolwindow
[Package Manager Console] Look for a parameter to specify the project. For some cmdlets I've seen -ProjectName and some use -Project
[Solution Explorer] Right-click the project you want, and use the graphical package manager window (Manage NuGet Packages...).
I've just installed EF 4.3-beta1 for the migrations goodness, and I can't get it working. The error I get:
PM> Update-Database -Verbose
Using NuGet project 'Project.Domain'.
Using StartUp project 'ProjectWebSite'.
System.InvalidOperationException: No migrations configuration type was found in the assembly 'Project.Domain'.
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.FindConfiguration()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()
at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
No migrations configuration type was found in the assembly 'Project.Domain'.
I've added a new column to 2 EF classes:
public class MasterInstance
{
public int MasterInstanceId { get; set; }
[Required] public string HostName { get; set; }
[Required] public string Name { get; set; } /* <-- THIS IS NEW */
[Required] public string ConnectionString { get; set; }
public virtual ICollection<MasterInstanceLocation> MasterInstanceLocations { get; set; }
}
And my DbContext looks like this:
public class ProjectDontext: DbContext, IProjectContext
{
public IDbSet<Installer> Installers { get; set; }
public IDbSet<MasterInstance> MasterInstances { get; set; }
public IDbSet<MasterInstanceLocation> MasterInstanceLocations { get; set; }
}
Any ideas? My EF classes & context live in a separate assembly (Project.Domain). I've tried running the update-database in the context of both the main website and the domain project, and I get the same error either way.
-- EDIT --
Solution found. It turns out, that you need to enable migrations for your project. You can do this by running Enable-Migrations in the NuGet console (make sure you have the right project selected - for me this was the project.domain project).
This walkthrough provides more information
Solution found. It turns out, that you need to enable migrations for your project. You can do this by running Enable-Migrations in the NuGet console (make sure you have the right project selected - for me this was the project.domain project).
This walkthrough provides more information
Sometimes, even if you have enabled migration, this problem can occur. It means that configuration file has been deleted. In this case, you can run
Enable-Migrations -Force
in the Package Manager Console. -Force parameter is to override migration configuration file.
If you had already enabled migrations and just started seeing this error after some windows updates, ensure you are using the same version of Entity Framework across all projects using the NuGet Package Manager.
Recent windows updates may have installed a newer version of Entity Framework into your active project.
Background: Around 16 Mar 2016, I started getting the "no migrations configuration type" error when trying to add migrations to a project where I had already enabled migrations and had successfully done migrations before.
I noticed that around March 10, a new stable version of Entity Framework 6 had been released.
If I specified the -ContextTypeName parameter in the enable-migrations command, I got an error indicating the migrations were already enabled.
Another error I got as I was troubleshooting indicated that the Configuration type was not inheriting from the System.Data.Entity.ModelConfiguration.EntityTypeConfiguration, even though it was.
That led me to believe different versions of the Entity Framework were conflicting.
Resolution:
1) Tools -> Nuget Package Manager -> Manage Nuget Packages for Solution
2) (Not sure if this step is necessary, but..) I updated my version of the Nuget Package Manager to the latest version. Also, after updating my version of Nuget Package Manager, I had to restart Visual Studio twice before the NuGet Command line would work properly.
3) Tools -> Nuget package Manager -> Manage Nuget Packages for Solution -> Search Installed packages -> Type Entity Framework
a. You may see more than one version of Entity Framework there.
b. Click Manage on each version of Entity Framework and ensure that your projects are using the SAME version of Entity Framework.
•Uncheck the version of Entity Framework that you are not using and for the version of Entity Framework you ARE using make sure it is checked across your projects that need it.
Again, as noted in step 2, I had to restart visual studio twice to get the NuGet Package Manager Console to work properly after updating my version of the NuGet Package Manager. I got an error starting the console the first time, and "exception calling createinstancefrom with 8 arguments could not load file or assembly EntityFramework" when running the enable-migrations command the second time.
Restarting visual studio seemed to resolve those issues, however.
For me, this error occurred because I had the wrong project selected in the Package Manager Console's "Default Project" in VS2019.
Even when migration are enabled, the described behavior can occur if an incorrect Default Project has been chosen in the drop-down menu of the Package Manager Console. A non-graphical way around is to expand the command you are using, and specify the correct project name with the parameter -ProjectName
Update-Database -Verbose -ProjectName TheCorrectProjectName
You might still get a warning like
Cannot determine a valid start-up project. Using project 'TheCorrectProjectName' instead. Your configuration file and working directory may not be set as expected. Use the -StartUpProjectName parameter to set one explicitly.
Nevertheless, this additional command line parameter solved the issue for me.