Can't add Entity Framework migrations when project is split - entity-framework

I am porting a project from MVC 5 to MVC 6. My project has two components the MVC application itself and a assembly that has all of the database code in it, including all db models, and the DbContext.
In the assembly I modified the project.json to have the ef commands dll and removed it from the MVC project.json. I have ported over all of my models, etc. and the application is compiling without errors. I want to execute
dnx ef migrations add
from a command shell in the subdirectory of the assembly in the project. When I do that I get
No DbContext was found. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.
What is the correct way to accomplish this type of project organization?

Make sure your run this command from the project.json that has your EF Commands and your DBContext. You will not be able to run this from the artifacts folder since dnx custom commands like ef are registered on a per project.json basis.
If you are not already on the latest beta, it would be a good opportunity to try it again on beta8 to make sure everything works.
PS: Not just dnvm upgrade but the tooling as well.
If nothing works, checkout this other question. You will need a Startup.cs but it doesn't need to run anything in particular. It just need able to configure the dependencies.

Related

Entity Framework 6.4.4 (.NET 4.7.2) - No connection string named

Good day,
I've created a C# .NET 4.7.2 class library project which integrates EF6. When the parent project makes any call to the DLL for db access, the above exception is thrown.
A common solution is to include a reference to EF in the parent project and to copy the connection string element from the class library's app config file. This indeed works, but is not ideal. I really envision all of these settings encapsulated within the DLL.
Anyone have a work-around?
Thanks.

Problem with Entity Framework 6 migrations

I have a problem with creating migration for Entity Framework 6 in code first approach. I already created migration, but later when I tried to create new, this was the errror:
Your target project 'Blog' doesn't reference EntityFramework. This
package is required for the Entity Framework Core Tools to work.
Ensure your target project is correct, install the package, and try
again.
I checked NuGet, EF6 is installed, alongside with these packages

Unable to add migration on Entity Framework

When trying to add a migration add-migration "Inicial" -Verbose , this error message appears:
No migrations configuration type was found in the assembly 'DevIO.UI.Site'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration).
I followed the message instruction and typed the command Enable-Migrations, but there was still an error
No context type was found in the assembly 'DevIO.UI.Site'
What can I do?
Assuming you already have a DbContext class in your solution, you need to know which project it is in. You should be adding migrations in that project. If you're using the Visual Studio package manager console for this, you will need to set the Default Project to the project which contains the DbContext (i.e. in the dropdown at the top of package manager console).
Entity Framework is different from Entity Framework Core. You are using Entity Framework (which we know because EF Core doesn't include an Enable-Migrations command), but your DbContext is coming from the Microsoft.EntityFrameworkCore namespace. If you replace using Microsoft.EntityFrameworkCore with using System.Data.Entity that should be the correct DbContext class for Entity Framework.
I figured that if you install a NuGet packages named Microsoft.EntityFrameworkCore.Tools it will solve this problem .

Installing EF to .NET core library project

I am new to .NET core. Trying to get used to it. But its kind of complicated.
I have my poco classes in 'solution.Entity' library project and I want to use them to create a database with EF code first in my 'solution.Data' library project.
But when I try to install Entity Framework via Nuget to my 'solution.Data' project it says 'Package Restore Failed. Rolling back...'
Both projects are .NET Core Library projects.
What do I need to do to be able to use EF in .NET core library projects? It's look like I need some kind of configuration.
Thanks for helps.
Make sure you're installing EF Core. .NET Core has it's own implementation of Entity Framework.
This should be the NuGet package you're installing.
There are lots of pieces to getting Entity Framework Core working right. I highly recommend taking a look at the docs. There are dozens of samples that show how to setup a project.
https://github.com/aspnet/EntityFramework.Docs
http://learn.microsoft.com/ef/
In particular, I suggest this guide. It will talk you how to take your POCO classes and make a database in a .NET Core project. You will likely find it easiest to keep.everything in one project for now and move code into separate libraries once you get more familiar with EF.

EF code migration in reference dll coming through nuget

We have a situation. Our company has Framework build on .net which is used by products developed using that Framework. Now this Framework is using EF code first. We want to implement EF Code Migration though generated migrations definitions because our entities are there in one of the Framework project. So I have implemented it. But the situation is this Framework we are distributing to other products as Nuget package (internal).
Now the situation is lets say ProductA is consuming the package which has xyz.dll which has migrations enabled in it. Now the developers of "ProductA" wants to upgrade the database created by Framework EF code first using that xyz.dll migrations, but this dll is only added as referenced dll. So running command in package manager console like Update-Database is not working because project is not in the current solution and its in reference dll
How do I solve this?
This is actually pretty easy to solve, the project which you add your nuget package to also needs to reference EF.
You can actually enforce this inside your nuget packages with a dependency eg:
<dependencies>
<dependency id="EntityFramework" version="4.2.0.0" />
</dependencies>
in your nuspec (obviously update the version with the one you are using)
See: http://docs.nuget.org/docs/reference/nuspec-reference#Specifying_Dependencies
and
http://docs.nuget.org/docs/reference/versioning#Specifying_Version_Ranges_in_.nuspec_Files
for some more details around how the dependancy syntax works