EF 6 with a dnx project - c#

I have a new ASP.net 5 dnx class library I am using for entity framework. I need to target EF 6 because some features I need are not in EF 7.
First the EF tools (like enable-migration) were not there. I added an old style class library and installed EF 6 and now the commands are there.
When I run enable migrations I get this error:
PM> Enable-Migrations
Exception calling "SetData" with "2" argument(s): "Type
'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject'
in assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation,
Version=14.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is
not marked as serializable." At
D:\Projects\Rebus\FMS\Code\Current\FMSSupport\FMSSupport\packages\EntityFramework.6.1.3\tools\EntityFramework.psm1:720
char:5
+ $domain.SetData('startUpProject', $startUpProject)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SerializationException System.NullReferenceException: Object reference not set to an instance
of an object. at
System.Data.Entity.Migrations.Extensions.ProjectExtensions.GetProjectTypes(Project
project, Int32 shellVersion) at
System.Data.Entity.Migrations.Extensions.ProjectExtensions.IsWebProject(Project
project) at
System.Data.Entity.Migrations.MigrationsDomainCommand.GetFacade(String
configurationTypeName, Boolean useContextWorkingDirectory) at
System.Data.Entity.Migrations.EnableMigrationsCommand.FindContextToEnable(String
contextTypeName) at
System.Data.Entity.Migrations.EnableMigrationsCommand.<>c__DisplayClass2.<.ctor>b__0()
at
System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action
command) Object reference not set to an instance of an object.
I have tried different options for enable-migration such as specifying a startup project or the contextTypeName.
Any ideas?

Following this recommendation, EF6 data projects should use the existing .NET Framework class library csproj type.
Even after following the recommendations, I was still getting the same error. Unloading your ASP.NET Core project(s) and then invoking the Enable-Migrations command on the appropriate csproj should allow the command to execute. You can unload a project by right-clicking on the .NET Core project and choosing the 'Unload Project' option. It should also be noted that the Package Manager Console window has a 'Default project:' drop-down selection. You should choose the project with your DbContext.

It's because ASP.NET Core introduced a new project type/configuration system (see package.config vs project.json) and the old EF tools don't know how to work with the newer system (project.json). It would probably be easiest to create a new project and port your code into it if you want to go back to EF6.
Interestingly (well, disappointingly actually) they're backtracking on project.json see:
http://xoofx.com/blog/2016/05/11/goodbye-project-json/

I had the same issue. Just setting the start project from ASP.NET Core to my .NET Framework Lib does the trick for me =) Now i can do EF6 migrations as usual "Add-Migration" command...
P.S. Sorry for my eng.

Related

Can't get migrations to work: Cannot bind argument to parameter 'Path' because it is null

I'm taking a course in MVC that was recorded in 2016, so maybe that has to do with this error. It says to open NuGet console and type in "Enable-Migrations" in order to enable Entity Framework. But when I type that I get all sorts of error messages and yes I double-checked to make sure I'm doing everything the instructor is. I tried different versions of EF in Nuget Package Manager and the error messages change but still happen.
PM> Enable-Migrations
Enable-Migrations : Cannot bind argument to parameter 'Path' because it is null.
At line:1 char:1
+ Enable-Migrations
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Enable-Migrations], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Enable-Migrations
First off, why is this even necessary? From what I've seen of EF and LINQ so far I'd rather code in machine language, but in courses I took before that used EF we never had to do "Migrations" before so I'd like to solve this error message but I don't even see the point of it. I thought I read somewhere that Microsoft in its infinite wisdom has changed the way we do migrations but can't find any updated documentation.
Type following command in package manager console.
dir
or
pwd
pwd show you the path of current directory.
dir list down all folders of current directory. if package manager console is not set to project directory, run
cd "YouProjectDirectory"
and run
Enable-Migrations
If you install required packages in this case entitty framework core packages, it will run successfully.

Pro Asp.Net 2016 nuget Update-Database cmdlet error

I am new to Asp.Net Core. While reading Adam Freeman's book on Asp.Net Core (2016)
I have done everything properly prior to my problem, however, I can not create database tables using Update-Database Project manager console command as said in the book. Similar topics did not help me. Here is the error.
Invoke must be called on a background thread.
Update-Database : The term 'Update-Database' is not recognized as the name
of a cmdlet, function, script file, or operable program. Check the spelling
of the
name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:1
+ Update-Database
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Update-Database:String) [],
Comman
dNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
What version of ASP.NET Core did you select when created the project?
Did you add both Microsoft.EntityFrameworkCore.SqlServer and Microsoft.EntityFrameworkCore.Tools packages, did you do that via the Nuget manager or by editing your .csproj and what versions of that packages did you add?
In the book he adds packages by editing project.json which is no more supported and is replaced by PROJECT_NAME.csproj.
Make sure your dependencies were added and there are no warning signs (In Solution Explorer, expand your project tree. Check if required packages are listed in Nuget section under the "Dependencies" and there is no yellow warning triangle sign on the item).

Entity framework 7, visual studio 2015, console commands don't work

I am using the ASP.NET 5 Web application Template (which is using EF7 and MVC 6), the one with the individual User Accounts, and I have the following problem:
When I add my own DBSet to the ApplicationDbContext class, and then going to a page which is trying to use this new DBSet, it doesn't work, I get this:
A database operation failed while processing the request.
SqlException: Invalid object name 'WallMessageModel'.
There are pending model changes for ApplicationDbContext
Scaffold a new migration for these changes and apply them to the database from the command line:
dnx . ef migration add [migration name]
dnx . ef migration apply
Now when I run these commands, or any "dnx" commands I get:
CategoryInfo NotSpecified: Error: Unable t...stemindsWebsite:String, RemoteException FullyQualifiedErrorId : NativeCommandError
And if im trying to run any "k" commands im getting:
CategoryInfo : ObjectNotFound: k:String, CommandNotFoundException FullyQualifiedErrorId : CommandNotFoundException
I'm using "EntityFramework.SqlServer": "7.0.0-beta5", "EntityFramework.Commands": "7.0.0-beta5"
I tried using the 7.0.0-* version and it still doesn't work for me. Can you please tell me what am I doing wrong?
Basically, I believe that EF commands are not installed properly on my machine (via Visual Studio 2015/PowerShell). However, there doesn't seem to be any reference that shows how to get those installed (shouldn't they be built into VS2015 when using EF7?).
When you add another DbSet to your ApplicationDbContext class or add/modify any other existing entity class, your runtime model changes from database model. Hence you need to scaffold a new migration and apply it to database so that both models are in sync with each other. As the exception message suggests, you should run those commands to do that.
For those commands (or any other dnx commands) not running, dnx commands are not built in VS2015. They come from Microsoft ASP.NET and Web Tools extension for visual studio. Please verify that you have it installed for your VS. That will enable you to run dnx commands. k is very old term and it was renamed to dnx. Also beta5 is old version, the latest version is RC1, which has many more features. Detailed information on how to install and use Web Tools is available at https://get.asp.net/

Enable Migrations in MVC throws File not Found Error

I'm new to using MVC, and when working through the tutorials regarding migrations in Visual Studio 2013 I appear to have broken the tie-in to EntityFramework in the Package Manager console.
I believe this started when I updated the EntityFramework to version 6.1.3, But that appears to be incompatible with the MVC Controller scaffolding. So I rolled it back to version 5.0.0.
When I now enter the command enable-migrations
I get the following error:
Exception calling "CreateInstanceFrom" with "8" argument(s): "Could not load file or assembly 'EntityFramework, Version=5.0.0.0
, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified."
At D:\ss\CAP\CAP_MVC_Dev_2\CAP_MVC\packages\EntityFramework.5.0.0\tools\EntityFramework.psm1:431 char:31
+ $domain.CreateInstanceFrom <<<< (
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodTargetInvocation
The file EntityFramework.psm1 exists where it is described above, but the error appears to be in that file? How is $domain.CreateInstanceFrom missing, and where from? Uninstalling and reinstalling the EntityFramework package makes no difference.
I've tried creating new projects from scratch and this error persists across them. An uninstall/reinstall of Visual Studio 2013 has not resolved the issue either, nor has destroying the connected database and starting again with an empty one.
So, it turns out the solution was similar to this one
Entity Framework code first migrations throwing error
When I first attempted this solution there were reports of gacutil not being recognised. Due to the environment I work it it is not simple to add the gacutil to be universally recognised (I believe), so i found the gacutil.exe in the file system, copied that to the lib\net45 folder referenced in the linked post, and then ran the command
gacutil /i EntityFramework.dll
This has resolved the problem, and i no longer receive the 'File not Found error'
Instead of installing with GAC, I came to the following solution (VS2013 SP5 + EF 5):
Copied (Solution dir)\packages\EntityFramework.5.0.0 (entire directory)
to
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\CommonExtensions\Microsoft\EFTools\NuGet Packages\
The NuGet Packages directory did not exist, so one needs to create it manually.

error: This operation would create an incorrectly structured document

I keep trying to install the entity framework 6 and it always rolls back with the error of
error: This operation would create an incorrectly structured document.
I've uninstalled all the references to every dll mentioned at this location. http://entityframework.codeplex.com/wikipage?title=Updating%20Applications%20to%20use%20EF6
The crazy thing is that i can create a new project, create a webAPI program, and attempt to add Entity Framework 6 and I get the same error, even after removing all references to System.Data.Entity.dll I'm already a few days into this, and needing serious help.
what can i do to get Entity Framework 6 to install?
Note: It will install just find to a class library just not a WebApi or MVC application.
If it helps here is some more detailed error info.
PM> Install-Package EntityFramework -Version 6.0.0
Installing 'EntityFramework 6.0.0'.
Successfully installed 'EntityFramework 6.0.0'.
Adding 'EntityFramework 6.0.0' to AC.
Successfully added 'EntityFramework 6.0.0' to AC.
System.InvalidOperationException: This operation would create an incorrectly structured document.
at System.Xml.Linq.XDocument.ValidateDocument(XNode previous, XmlNodeType allowBefore, XmlNodeType allowAfter)
at System.Xml.Linq.XDocument.ValidateNode(XNode node, XNode previous)
at System.Xml.Linq.XContainer.AddNodeSkipNotify(XNode n)
at System.Xml.Linq.XContainer.AddContentSkipNotify(Object content)
at System.Xml.Linq.XContainer.Add(Object content)
at System.Data.Entity.Migrations.Extensions.XContainerExtensions.GetOrCreateElement(XContainer container, String elementName, XAttribute[] attributes)
at System.Data.Entity.ConnectionFactoryConfig.ConfigFileManipulator.AddOrUpdateConfigSection(XDocument config, Version entityFrameworkVersion)
at System.Data.Entity.ConnectionFactoryConfig.InitializeEntityFrameworkCommand.<>c__DisplayClass3.<Execute>b__1(XDocument c)
at System.Data.Entity.ConnectionFactoryConfig.ConfigFileProcessor.ProcessConfigFile(ProjectItem configItem, IEnumerable`1 manipulators)
at System.Data.Entity.ConnectionFactoryConfig.InitializeEntityFrameworkCommand.<>c__DisplayClass3.<Execute>b__0(ProjectItem i)
at System.Data.Entity.ConnectionFactoryConfig.ConfigFileFinder.FindConfigFiles(ProjectItems items, Action`1 action)
at System.Data.Entity.ConnectionFactoryConfig.InitializeEntityFrameworkCommand.Execute()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Uninstalling 'EntityFramework 6.0.0'.
Successfully uninstalled 'EntityFramework 6.0.0'.
Install failed. Rolling back...
Install-Package : This operation would create an incorrectly structured document.
At line:1 char:16
+ Install-Package <<<< EntityFramework -Version 6.0.0
+ CategoryInfo : NotSpecified: (:) [Install-Package], RuntimeException
+ FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand
I had the same problem. It turned out that I was adding EF to one of my really old projects and web.config had this namespace defined for <configuration /> element:
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
Removing that namespace declaration (xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0") helped and solved this issue.
I had the same problem, and discovered the cause was the element in the web.config file. In my case, I changed this:
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
to this:
<configuration>
That fixed it.
When Entity Framework is upgraded, the app.config or web.config files will get stuff written into them. I suspect that might be the problem. Take a look at your app.config or web.config files. Maybe the EF upgrade is having trouble writing to them without making an invalid xml document.
It is to do with your configuration files. Just to detail the solution I found:
First save your app.config, packages.config and web.config
Then delete them all. I actually deleted app.config and packages.config and just cleared out the lines between and in the web.config.
Then I ran the Install-Package again, all went well, then I added back in my configuration.
In a web app, web.config had a reference to external app.config:
<appSettings configSource="App.config" />
This requires the App.config to be in a particular format, that EntityFrameworks installer considers invalid
So, like others, the key was to:
rename/delete the App.config
Install EF
bring back the App.config
I had the same issue and I modified my web.config from
(<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">)
To
(<configuration>)

Categories

Resources