How to keep domain model separate from repository and enable migrations - entity-framework

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.

Related

How to apply EF Core DB schema from a shared class library NuGet package

Let's say I have a class that extends DBContext (hereby called DBContext for simplicity) defined in a shared library plus an extension method to simplify adding this DBContext to dependency injection (given a configuration variable, it will generate a connection string to the database).
How would I go about applying the database schema? From the shared library? From a binary that consumes this library?
It's my understanding that when you apply a schema to a database a migrations folder is created and I want one canonical place where these migrations go.
If I run dotnet ef database update from the shared library, how does it know where the database is if I'm not providing configuration? Where would I provide configuration (like which database server and what credentials)
This shared library will likely be used by many consumers, so having migration scripts on each project sounds like a bad idea. Any suggestions?
Place the Connection String inside configuration for your Startup project (appsettings.json or other) and pass it to your extension method in your library.
Leave the Migrations in the library project with the DbContext. When you run dotnet ef database update you can provide separate --project (the project with your DbContext and Migrations) and --startup-project (the project that actually consumes your library) options.
https://learn.microsoft.com/en-us/ef/core/cli/dotnet#using-the-tools
The startup project is the one that the tools build and run. The tools have to execute application code at design time to get information about the project, such as the database connection string and the configuration of the model. By default, the project in the current directory is the startup project. You can specify a different project as startup project by using the --startup-project option.

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.

Entity Data Model Wizard requires app.config and ignores custom T4 templates

Using EF6 and Database First, I created custom T4 templates (*.tt files) in my VS2013 class library project. I don't want to migrate the app.config file with my assembly so I delete it. When I Update Model from Database, it presents me with a connection to select (I believe this is stored in the Visual Studio user preferences). If I choose not to store this in the app.config, it continues to show this step in the Wizard. Whenever the connection selection step is presented and the wizard completes, I notice the EDMX generates the two default *.tt files alongside my custom ones. Any way of preventing this? I read an article about EF code-based configuration and I tried DbConfiguration.SetDefaultConnectionFactory but that didn't help. The EF wizard always wants a connection in the app.config. Is there any way around this?
I ended up leaving it in the app.config (on my machine) and do not migrate this file to our development/test/prod environments. It's become a manual task to confirm each app.config file since it has become necessary to migrate some files due to adding project assembly redirects.

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.