Using EF core and EF6 side-by-side - entity-framework

I have a .NET Core 3.0 solution that contains 5 projects. One of the projects uses EF6, and another one uses EF Core, so EF6 and EF Core are being used side-by-side. Visual Studio is as fresh as possible (16.3.5). Trying to create migration for the project that uses EF6 and executing the Add-Migration command on my laptop with Windows 10 I see a warning message: "Both Entity Framework 6 and Entity Framework Core are installed. The Entity Framework 6 tools are running. Use 'EntityFrameworkCore\Update-Database' for Entity Framework Core", but all goes fine and migration completes successfully.
The Get-Module command displays the following table:
---------- ------- ---- ----------------
Script 6.3.0 EntityFramework6 {Add-EFDefaultConnectionFactory, Add-EFProvider, Add-Migration, Enable-Migrations...}
Script 3.0.0 EntityFrameworkCore {Add-Migration, Drop-Database, Enable-Migrations, Get-DbContext...}
Script 2.0.0.0 NuGet {Add-BindingRedirect, Find-Package, Get-Package, Get-Project...}
Script 0.0 profile
On another workstation with Windows 7 onboard, running the same command I see almost the same table but with empty list of cmdlets exported by EF6:
---------- ------- ---- ----------------
Script 6.3.0 EntityFramework6
Script 3.0.0 EntityFrameworkCore {Add-Migration, Drop-Database, Enable-Migrations, Get-DbContext...}
Script 2.0.0.0 NuGet {Add-BindingRedirect, Find-Package, Get-Package, Get-Project...}
Script 0.0 profile
Eventually, in such a case I can't perform migrations on EF6-bound project at all. How could I fix that? Neither 'Add-Migration' nor 'EntityFramework\Add-Migration' nor 'EntityFrameworkCore\Add-Migration' won't work.
UPDATE 17 oct 2019
That seems to me that my VS installation is corrupted. The good news is that EF6.3 comes with the ef.exe utility that replaces migrate.exe and allows a developer to create migrations using command line outside VS IDE.

Create an additional solution(s) with only your EF6 or only your EFCore dependent project(s) and do what version-specific actions need to be done while using that solution.

Related

Entity Framework 6 .net Framework Migrations / Package Management Console - How Do You Run These In An Azure Pipeline?

I am setting up an Azure Release Pipeline and I need to execute any pending DB Migrations as part of the release.
I have been scouring the internet for over an hour and everything I can find is about dotnet Core, while the database is EF6 on .Net Framework, not dotnet Core (I've done this several times before for Core).
The problem, as I see it, is that EF6 works using Visual Studio's built in Package Manager Console - This just doesn't exist in an Azure Pipeline; It's a Visual Studio weirdness.
There seems to be several ways I can skin this cat, in my head, but I can't figure out how to start with either of them within the context of the pipeline...
OPTION 1: Run the Migrations on the Pipeline - but... how?
OPTION 2: SQL Scripts - Requires running the Package Manager to generate them so they can be run (if I could do that on the pipeline then I'd just run it anyway so these would have to be created locally and committed with the code which is somewhat backward as a solution IMO)
OPTION 3: Write a console app - Do I really have to do this??
You can try Entity Framework Migration Extensions.
This task allows a Build / Release to provide database connection parameters and execute an Entity Framework 6 migration against the database.
Build your project to an output folder and include the migrate.exe executable that comes with Entity Framework 6.
Create an automated build that packages up your files and makes them accessible during a Release.
Create a Release definition for the relevant Build
Add an EF6 Migration task. Once that task is added to an environment within the release, you'll need to enter the appropriate parameters to configure it. All file path parameters should be within the file system for the build, none of them are for TFS source control paths.
You can also check this article.
The answer here is to use the ef6.exe command line tool and make sure it gets shipped with your build.
This could be useful to anyone here until Microsoft update the non-existent docs: http://github.com/dotnet/EntityFramework.Docs/issues/1740 - This contains a table with a kind of translation matrix between the two.

How apply Entity Framework Migration on production Server

I am working with Entity Framework code first.
I have managed to work with migrations, but i need to type commands in the Package Manager Console.
(for example Update-Database command).
It works fine, but it works on my developpemnt computer.
Now, imagine i have a lot of production server. Some of those servers are still in database version 1, others in version 3 and the lastest version is 5.
Is it possible to run the equivalent of Update-Database Command from C# Code ?
DbUp is one other open source tool which is effective and provides robust features to perform production DB upgrade in EF Code first approach.
Here is an informative article comparing the Dbup open source tool vs EF migration feature.
Yes,you can do that.You have to change your web.config file's connection string according to your production server and then run :
PM> Update-Database
Update : to generate scripts.
PM > Update-Database -Script -SourceMigration: $InitialDatabase -TargetMigration:
AddPostAbstract
You can refer this for more info : Getting a SQL Script

Update-Database is obsolete - Visual Studio 2015 Update 3

I've done the upgrade for Visual Studio 2015 Update 3, One project is using ASP.Net Core but only for front end JS and CSS using gulp etc. I've just tried to run migrations on the project with migrations and get the following:
Update-Database is obsolete. Use Apply-Migration instead.
Using project 'ProjectName'
Build started...
Build succeeded.
Using start-up project 'MyProject'.
Using application base 'pathlocation'.
Using application configuration 'pathlocation'\Web.config'
Using data directory ''pathlocation'\App_Data'
Using assembly 'My.dll'.
Exception calling "CreateInstanceAndUnwrap" with "8" argument(s): "Could not load file or assembly 'EntityFramework.Commands' or one of its dependencies. The system cannot find the file specified."
I can't see that I've upgraded to EF 7, and I've uninstalled and reinstalled EF 6 in the project. Any suggestions as to what I'm missing?
I'm sure this was something to do with .Net Core messing up the tooling. To resolve I ran VS as admin. Removed all the EF 7 references in the packages.config in the .Net Core project. Then uninstalled and reinstalled EF 6 through Package Manager Console:
Uninstall-Package EntityFramework -Force
Install-Package EntityFramework -Pre
Using EntityFramework 7? Did you set your dnvm?
first you have to run from the cmd of your project folder this command:
dnvm upgrade
Then try again to use migration, but only from the cmd, and using the dnx command.
Example:
"dnx ef migrations add Initial"
more info: here

Can't compile project after importing stored procedure

After importing a stored procedure into my datamodel the project stopped compiling.
It keeps giving me error:
The best overloaded method match for
'System.Data.Entity.Core.Objects.ObjectContext.ExecuteFunction<TicketDataModel.sp_get_orphanjobgrades1_Result>
(string, params System.Data.Entity.Core.Objects.ObjectParameter[])'
has some invalid arguments C:\Users\nikolaev\Documents\MySoft\TicketManager-06 11 2013\TicketManager\TicketDataModel\TicketDataModel\TicketEntities.Context.cs 105 20 TicketDataModel
and
`Argument 3: cannot convert from 'System.Data.Objects.ObjectParameter' to
'System.Data.Entity.Core.Objects.ObjectParameter'
C:\Users\nikolaev\Documents\MySoft\TicketManager-06 11 2013\TicketManager\TicketDataModel\TicketDataModel\TicketEntities.Context.cs 79 143 TicketDataModel`
The code in context.cs is as follows:
public virtual ObjectResult<sp_get_orphanjobgrades1_Result> sp_get_orphanjobgrades1(Nullable<System.DateTime> start_date, Nullable<System.DateTime> end_date)
{
var start_dateParameter = start_date.HasValue ?
new ObjectParameter("start_date", start_date) :
new ObjectParameter("start_date", typeof(System.DateTime));
var end_dateParameter = end_date.HasValue ?
new ObjectParameter("end_date", end_date) :
new ObjectParameter("end_date", typeof(System.DateTime));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<sp_get_orphanjobgrades1_Result>("sp_get_orphanjobgrades1", start_dateParameter, end_dateParameter);
}
I read that this may be because of EF 6 and that one needs to download VS 2012 Update 1 and/or EF 6 Tools for VS 2012. I downloaded the tools and I already have update 4 installed, but that doesn't help.
Why is this?
The problem is that your model don't know anything about new parameters. I guess that it happens when your model using EntityFramework (EF) lower version (5.0 for example), but in code you are trying to use EF 6.0.
So, you should either downgrade your code to EF 5.0, or upgrade your model to 6.0.
Upgrading model:
Open Tools -> NuGet Package Manager -> Package Manager Console;
Choose project, that contains your model as Default project on the
top of Console.
Type "Uninstall-Package EntityFramework" and press Enter;
After removing, type "Install-Package EntityFramework -version
6.0.0" and press Enter again;
Go to YourModelName.Context.cs and YourModelName.Context.tt and replace "using
System.Data.Objects; using System.Data.Objects.DataClasses;" with "using
System.Data.Entity.Core.Objects;". Also, you may need to make the
same in all files, where this problem appears.
Or you can downgrade EF version, used in your code. For this, you should do all the same in first 3 steps but in the fourth replace "-version 6.0.0" with "-version 5.0.0". The 5th step is not needed for this.
To fix this issue:
If version conflicts, install package for respective version of EF.
If version is same, change the above namespace to System.Data.Entity.Core.Objects
You can download new verions in msdn.
Download Tools for Entity Framework 6.1.0 or new versions
in http://www.microsoft.com/en-us/download/details.aspx?id=40762
Use in Package Manager Console
Install-Package EntityFramework -version 6.1.0 and enter
Restart your visual studio.
4.Check your package Manager and finish.
I just want to add that this happened to me after starting a brand new project. The issue was that I had created a new project, and then proceeded to update the Entity Framework, ASP.Net MVC, and other packages through the NuGet Package Manager. So, the code generated for my project no longer matched what I had installed.
The quickest solution for me was to just create a new project since I hadn't done any real work yet. I hope this helps somebody else out there.
My situation was a new project, and I went to nuget with Install-Package EntityFramework which installed the 6.0 version, however apparently my project was already set up with an earlier version? Not sure how, maybe the default of MVC 4.0 project...
So I had to do an Uninstall-Package -force EntityFramework followed by
Install-Package EntityFramework -version 5.0.0
Everything worked after that.

Error when Reverse Engineer Code First with EF Power Tools Beta 2 and VS2012 Update 1

Just trying to generate POCO objects, context etc for an existing database by using the EF Power Tools Beta 2 "Reverse Engineer Code First" facility.
I'm using VS2012 Ultimate with Update 1, on Windows 7 64 bit.
I point it at my existing database and hit OK. I get the following error in my Build Output window:
One or more errors occurred while processing template 'Entity.tt'.
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\EF.Utility.CS.ttinclude(1,4) : error : A processor named 'T4VSHost' could not be found for the directive named 'CleanupBehavior'. The transformation will not be run. The following Exception was thrown:
System.InvalidOperationException: Cannot find processor for directive 'T4VSHost'.
at Microsoft.DbContextPackage.Utilities.EfTextTemplateHost.Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost.ResolveDirectiveProcessor(String processorName)
at Microsoft.VisualStudio.TextTemplating.Engine.ProcessCustomDirectives(ITextTemplatingEngineHost host, TemplateProcessingSession session, IEnumerable`1 directivesToBeProcessed)
Any ideas?
Looks like it is a confirmed issue with Entity framework and Visual Studio 2012 update 1
The link does currently contain 2 work arounds
This is a legitimate issue on the EF Power Tools, but it's not related to the CTP issue, which was resolved in the final VS Update release. A fix for the EF Power Tools is in the works.
David Obando
Entity Framework Team
i also hit this same problem. The culprit was a package called Entity Framework Power Tool Beta 3. Updated this and everything was back to normal.