I have a project call RemoteWork where i have my Dbcontext. I have 2 models inside the DbContext - USER and PRODUCTS. I was using code first approach. I then used the "add-migration" and it was successful. I then referenced the Remotework in another project call Apiclient. They are both in the same solution. Now I have altered my table in USER and so it was out of sync. I wanted to make it to be in sync. I have tried different methods i have read online, I was getting different error messages. Can anyone please help in this regard.
I have done this: Add-Migration SecondMigration
This is the error message:
No DbContext was found in assembly 'AppClient'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.
Please note that AppClient was made as the startup project. I have also tried making remotework as a start up project, it did not resolve it either:
PM> Add-Migration SecondMigration
No DbContext was found in assembly 'AppClient'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.
PM> Add-Migration -Name MyMigration -OutputDir MyMigrationDir -Context BettingDbContext -Project RemoteWork -StartupProject AppClient
I wanted the models to be in sync with my database
I assume you are running add-migration from the package manager console in visual studio.
Either make sure you have selected the project that contains your context in the "Default project" dropdown at the top of the package manager console. Or use the -StartUpProjectName argument on add-migration.
Related
I have a web api project "Reactivities" and another project "Persisitence" to place my DbContext. When I try to use my add-migration in the Reactivities, using this syntax in the console add-migration InitalCreate -Context .\Persistence I get this error:
No DbContext named '.\Persistence' was found.
Even if I use add-migration InitialCreate -Context .\Persistence\DataContext still I get the same error,
This is in my startup class in Reactivities
services.AddDbContext<DataContext>(opt =>
{
opt.UseSqlite(Configuration.GetConnectionString("DefaultConnection"));
});
How can I correct this?
You have to do migration work in a project that instantiates the dbcontext, in my case I have created a dbmigrator project with a class that inherits the
IDesignTimeDbContextFactory<T>
where T is the dbcontext type.
This allows me to have a project that has the dbcontexts, a project that works with migrations and a project that uses the dbcontexts independently of each other.
This guide shows you how to use the interface i mentioned:
https://codingblast.com/entityframework-core-idesigntimedbcontextfactory/
I have a clean project solution (intended to be a .NET Core MVC app)
I have two projects - Home.CorePoC.EntityModel and Home.CorePoC.Model
I am calling dotnet cli from Home.CorePoC.Model.
The idea is that I want partial classes and logical separation from custom attributes and attributes from database.
I want to write a PowerShell script that I will be calling when I want to add/remove some tables, columns in tables etc.
In dotnet CLI when I add initially tables, it works just fine:
Scaffold-DbContext "Server=sqlServ2016;Database=TestDb;Persist Security
Info=False;User ID=admin;Password=admin;" Microsoft.EntityFrameworkCore.SqlServer
-Tables "Table1","Table2","Table3" -ContextDir "..\Home.CorePoC.EntityModel"
-OutputDir "TestDB" -force -DataAnnotations -UseDatabaseNames
Now, when I want to add just more tables it work as well but I have an error message: "Cannot add 'Table4'. There is already a linked file in this folder with the same name."
Also, I get error in cli console:
Exception calling "AddFromFile" with "1" argument(s): "A drag operation is already in progress (Exception from HRESULT: 0x80040103)"
Must I delete all files and then recreate it (linked file TestContext.cs that was initially created) or there is different way how to avoid this error message which is non-critical (I successfully added Table4 to my models).
I am planning to put this statement into PowerShell script which it will be called from project folder (where I have installed nuget Microsoft.EntityFrameworkCore.Tools(2.1.4))
That is because it will try and add the Context file for Table4 but is unable to as one already exists. There is already a feature request to update Entity Framework Core scaffolds rather than having to recreate them every time. You would have to override the generated entities each time by using the -Force flag
I'm trying to run a add-migration via my .net MVC project. There are two DBContext in a DBContext folder. However, when I run the command:
add-migration -ConfigurationTypeName xxxxx.Configuration -Name SavedSearches
it tells me:
The migrations configuration type 'xxxxx.Configuration' was not be found in the assembly 'xxxxx'.
Where do I find the proper name/namespace for the configuration type (i.e. what folder is it in, what level)? I've tried to search, but nothing clearly states it out.
Thank you.
From the Tools menu, click Library Package Manager and then Package Manager Console.
At the PM> prompt enter the following commands:
enable-migrations
add-migration InitialCreate
update-database
Following this Microsoft Tutorial when I run the PM> Add-Migration MyFirstMigration command in VS2015 project created from the same tutorial I get the following error that I can't resolve:
More than one DbContext was found. Specify which one to use.
Use the '-Context' parameter for PowerShell commands and the
'--context' parameter for dotnet commands.
Point to note
I'm using the above tutorial with the exception that I'm using Individual User Account authentication instead of No Authentication used in the tutorial.
I've latest release of ASP.NeT Core 1.0 and VS2015-Update 3 on windows 8.1
This is a freshly created project. No other DbContext was manually installed
Running the following command (obtained from this article) and a response from #Maverik (from StackOverflow here) and a suggestion from #doctor above helped me resolved the issue. Thank you all for your help:
PM> Add-Migration MyFirstMigration -Context BloggingContext
The error clearly explains to mention --context with db Context name if more than one DbContext. So try by mentioning your DbContext name.
dotnet ef migrations add Initial --context SampleDbContext
Hope this helps.
If you need only update a identity schema existent, try it:
update-database -Context ApplicationDbContext
ApplicationDbContext = your identity context
that because you have two DbContext in your solution. First is default created when you creating project(ApplicationDbContext) and second your EF DbContext.
Solution is described in error message just specify your EF DbContext
Add-Migration MyFirstMigration -Context DbContextName
It does work in my project.
Use below to commands:
PM> Add-Migration MyFirstMigration -Context YourDbContext
PM> update-database -Context YourDbContext
[--context]
The DbContext class to use. Class name only or fully qualified with
namespaces. If this option is omitted, EF Core will find the context
class. If there are multiple context classes, this option is required.
Add-Migration MyMigration -context DataContextName
I'm using VS Express 2013 .Net 4.5. I'm designing in MVC5 and EF6 with MS SQLServer LocalDB in an Oracle VirtualBox Windows 7 64bit client.
I am trying to apply SQL Server LocalDb migrations with the command:
PM> Enable-Migrations -ContextTypeName SGHWA_MVC.Models.Context
This always fails.
My limited web knowledge is with Web Forms so I am completely new to MVC and EF. Also I have never used PMC and Powershell. I have searched for solutions but have not found questions similar to this error that Package Manager Console produces.
First there is a warning:-
Cannot determine a valid start-up project. Using project 'SGHWA_MVC' instead. Your configuration file and working directory may not be set as expected. Use the -StartUpProjectName parameter to set one explicitly. Use the -Verbose switch for more information.
PCM drop-down box shows the Default project correctly as SGHWA_MVC. The solution property pages show this one project as the start-up project.
I went to http://docs.nuget.org/docs/reference/package-manager-console-powershell-reference but could not see the -StartUpProjectName parameter mentioned. I'm not sure to which command this parameter applies.
Then the first error appears:-
Exception calling "LoadFrom" with "1" argument(s): "Could not load file or assembly 'file://\W7O2007\Users\Admin\Documents\Visual Studio 2013\Projects\SGHWA_MVC\packages\EntityFramework.6.1.0\tools\EntityFramework.PowerShell.Utility.dll' or one of its dependencies. Operation is not supported.
(Exception from HRESULT: 0x80131515)"At \W7O2007\Users\Admin\Documents\Visual Studio 2013\Projects\SGHWA_MVC\packages\EntityFramework.6.1.0\tools\EntityFramework.psm1:780 char:62
+ $utilityAssembly = [System.Reflection.Assembly]::LoadFrom <<<< ((Join-Path $ToolsPath EntityFramework.PowerShell.Utility.dll))
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
It suggests the file is not there to be loaded but it does exist on the PC at the path shown. I suspect it is the Join-Path that fails but am unsure how to test this.
The packages.config files has a line:-
(leading < removed) package id="Powershell.Deployment" version="1.1.0.0" targetFramework="net45" />
My research shows it could be permissions, remote access, that the Project Build should have Generate Serialization Assembly as 'On', 'Off' or 'Auto' or even NuGet packages that cause this but I cannot determine if it is a PMC, NuGet, Powershell or Windows problem.
Two more error messages appear relating to similar problems in EntityFramework.psm1 and seem to be related to this first problem.
Please can anyone advise the likely cause and guide me through what/how to test and solve this?
I realize this is an old thread, but I just ran into this problem using a newly-installed copy of Microsoft Visual Studio 2015 Enterprise with Update 1.
My solution was to delete this folder:
C:\Users\\(my user ID)\\.nuget\packages\EntityFramework\6.1.3
and allow it to be regenerated the next time the Entity Framework is added to a project. Originally, the PowerShell DLL's were missing from this folder. Strange.
Your question has several parts.
First, you cannot find the documentation, and you have it at your fingertips:
Update-Database -?
Then, you can read at the bottom of this help:
TO see the examples, type: "get-help Update-Database -examples".
For more information, type: "get-help Update-Database -detailed".
For technical information, type: "get-help Update-Database -full".
Second, -StartupProjectName is required to find the configuration file that has the required connection string (you also specify the connection directly using another options) NOTE: This has nothing to do with the solution's startup project
And, if having this clear doesn't solve the problem, try uninstalling and reinstalling the EntityFramework package (in case something went wrong). Take into account that, if you have several projects, it's possible that you need to do this for all the affected projects. You can do this more easily using the Manage Nuget Packages for Solution... menu option.
Edit
(Added to help choose the right Default project in Package Manager Console or specify -ProjectName).
I forgot to answer the most important part of the question: for Migrations commands to work, it's necessary to run them on a project that references the EF assemblies. This solves the missing assembly problem.
In this case it's clear that the chosen StartUpProject, which is the Web App have the connection strings, but doesn't have a reference to EF. That's why running the command fails. And most probably the command is being run with this as the default project in the console.
To solve this, you need to run Migrations within a project which has references to EF, and it will work flawlessly: choose that project in the drop down list named "Default project" on the top bar of the Package Manager Console.
This can also be done by specifying the -ProjectName option, which must point to the project containing the Migration configuration class, and thus, have also the reference to EF. NOTE: you must also sepcify the -ConfigurationTypeName param if you have several migrations for several contexts, as supported from EF 6.