Issues adding second context to solution - entity-framework

I have multiple projects in my solution. What I am trying to do is get each plugin to have its own context.
When calling Enable-Migrations on the second project (making sure the drop down has my project selected), I get No context type was found in the assembly 'Plugin.Test'.. I do have a class in this project deriving from DbContext so am a little unsure as to what it wants.
This is the context in my second project:
public class SecondContext : DbContext, IDbContext
Could someone shed some light on how it's looking for a context so that I can make the changes to my second projects so it can be found?
p.s My first project EF integration is fine, this is purely when trying to add another context.

You can specify the context name:
Enable-Migrations -EnableAutomaticMigrations -ContextTypeName
NamespaceOfContext.ContextA -ProjectName ProjectContextIsInIfNotMainOne
-StartupProjectName NameOfMainProject -ConnectionStringName ContextA
For example:
Enable-Migrations -ContextTypeName SecondContext

Related

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 .

How to keep domain model separate from repository and enable migrations

I've a scenario where the project structure as following
DomainModels
Repository --ReferenceTo 'DomainModels'
Curator --ReferenceTo 'Repository'
MVC project -ReferenceTo 'Curator'
Now the problem is If I keep My DbContext in DomainModel which I'm supposed to keep, I cann't enable Db Migrations.
-- The only solution I've come across is to give the reference of 'DomainModels' to 'MVC projects'
using Enable-Migration MigrationName SomeAdditionalParameter here
Why is this a problem? I have a Data project which contains the models and DbContext. My web and business projects reference this. When I run the Add-Migration step, I just select the Data project in the project dropdown and it uses the connection string in the web.config in the web project. It works well and I have no problems.

Can't add Entity Framework migrations when project is split

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.

Why is AutomaticMigrationsDisabledException thrown?

We have
An Old Web Project
A New Web Project
A Class Library containing all Entity Framework code
All three projects reference Entity Framework 6.1.3.
The Class Library is shared by Old Web Project and New Web Project. Both web projects have a file reference to Class Library. They both point to the same location on disk.
Both web projects are configured to use the same database.
When I run Old Web Project in Visual Studio, it runs just fine.
When I try to run New Web Project in Visual Studio, it throws an AutomaticMigrationsDisabledException.
When I temporarily add ClassLibrary.csproj to the New Web Project's solution and run
Add-Migration TestMigration -ConfigurationTypeName MyConfiguration -ProjectName ClassLibrary -StartupProjectName NewWebProject -ConnectionStringName MyContext
it scaffolds a migration with empty Up() and Down() methods, indicating it did not find changes after all.
What might cause New Web Project to throw an AutomaticMigrationsDisabledException under these circumstances? How can I further diagnose the issue?
I had very carefully checked that New Web Project's file reference to ClassLibrary.dll is the correct one and that the DLL in question is up-to-date.
As a test, I added a new method to a class in ClassLibrary.dll, and found that New Web Project could not resolve it.
I deleted the existing file reference and added it back in, and the problem resolved.
For an unknown reason, an older copy of ClassLibrary.dll was being referenced in contrast to the information seen in the Properties window for that reference.

Code first migrations - what connection string will it use?

Code first migrations have been working very well for me. I have a services project and a wpf project. The model is in the services project which is referenced by the wpf project. Update-database is done on the services project, but uses connection string from the wpf project. I now add a web project which also references the service project. So now that there is a connection string in the app.config and there is one in the web.config, which one will it use?
In my scenario, the app.config in the services project is ignored. Code first migrations will use either the app.config from the WPF project or the web.config on the web project, depending which is selected as the startup project.
When doing update-database you should specify the project that contains the migrations. Make sure that you have an app.config file in that project that contains the correct connection string.
you can do a Update-Database -ConnectionStringName "MyConnectionString" and it should work like a charm.