Entity Framework Change Assembly Model not recognised - c#

I have an MVC 5 website with using the entity framework V6.1.1. The entity framework DbContext classes and models were originally all inside the website project. This project had 3 DbContext classes and 3 databases. I had also enabled migrations and applied one one of these databases.
I have now moved all the entity framework classes including the models and the migrations to a separate project and since then I have been getting the following error for the databases where a migration has been applied:
The model backing the 'MyContext' context has changed since the
database was created.
The database has not changed. I have also made sure the Context Key is the same in both the Configuration constructor and the database __MigrationHistory table.
I have also been seeing the following behavior:
I do a Get-Migrations in the package manager console and the correct migrations are returned. Then I am able to build and run the site and no error message is shown until I next make a change and build the solution.
If I change the Context Key in either the database of the Configuration constructor there is no error, but I assume that the migrations are not all being picked up.
I have also been looking through all the migrations files including the designer files and the namespaces all match up. If anyone could shed some light on this problem it would be much appreciated.

You should put this in the constructor of 'MyContext'
> Database.SetInitializer<YourDbContext>(null);

Related

How to use Code First Migrations When Domain Models in Separate Project from WebUI?

I have a VS 2017 solution with 2 .Net Core 1.1 projects, 1 is just a class library containing all of my domain models and the other is the actual MVC web application containing the contexts (the ApplicationDbContext & one I created) & all of the EFCore assemblies. I'm trying to enable & use migration on the context I created but having trouble being that the context is in the WebUI project and the models are in the class project. Upon 1st execution of Add-Migration command, I got this error about my target project didn't match my migrations assembly, so I figured out how to get around that by changing the migration assembly in Startup.cs. Add-Migration ended up working but the migration file was created in the class project where the EFCore assemblies are not referenced, thus giving an error on the migration file. I thought maybe I'd try and trick it by moving the file to my WebUI project to update the database, but then figured that may not work & there has to be an easier way of doing this. Does anyone know how to setup code first migrations for EFCore to keep track of modifications and update the database when my domain models are in another project? Any help would be greatly appreciated. Hopefully this isn't too vague. If more info is needed, I'll be all too happy to post. Thanks.
I may not be understanding the question correctly, but have you tried referencing your domain project in your webUI?

EF7 migration generator is ignoring context parameter

I have recently migrated my EF7 project from DotNet Core 1.0.0 to Version 1.1.0, now I have trouble with creating the database migrations.
In my project I have multiple contexts, the tool dotnet-ef is providing a command line parameter to distinct between these contexts in order to create a separate migration for each context.
When I try to create the migrations for the context "AuditLogContext" with the command
dotnet ef migrations add Initial --context 'Some.Namespace.AuditLogContext' --output-dir './Migrations/AuditLogMigrations'
then it fails, telling me
The name 'Initial' is used by an existing migration.
when I'm trying to create the migration with another name, say Initial2, the migration get created but for the wrong context.
In the folder
./Migrations/AuditLogMigrations
are new files named
20170212122451_Initial2.cs
20170212122451_Initial2.Designer.cs
CoreContextModelSnapshot.cs
I expect the file AuditLogContextModelSnapshot.cs instead.
Does anybody have solved this issue before?
I found the reason for this problem.
I have multiple contexts in my project, every context expects parameters. So migrations builder expects a factory for every context.
In my case one class, implements all factories, now there is a bug that the concrete order of the interfaces is used to determine which migrations are created.
public class Program : IDbContextFactory<CoreContext>, IDbContextFactory<AuditLogContext>, IDbContextFactory<LockContext>
This snippet will create migrations for the CoreContext
public class Program : IDbContextFactory<AuditLogContext>, IDbContextFactory<CoreContext>,IDbContextFactory<LockContext>
and the snippet will create migrations for the AuditLogContext.
It's a bit annoying but if you know how to create the migrations it's ok.

Scaffolding in .Net Core with Multiple Projects in Solution

I have created an .Net Core MVC6 application targeting net461. I have used a project structure I am very familiar with in which I place the data, model, and service classes in separate class library projects and the Web project references these.
When I attempt to scaffold a controller I receive an error that multiple matching types exist for the model I am scaffolding.
If I move all code to a single project, scaffolding is successful. If I move the context to Web project and leave the model in a separate project, I receive an error the NO matching types were found.
Has anyone else seen this same issue? Is there a workaround to still use this type of architecture?
Update
I started another project and always get this issue. I get this error when only using 1 extra project for the models. Attached is the error I recieve.
Scaffolding Error
Update 2
When the context and model are in the same project I receive this error.
Error editing dbContext
Cannot post a comment, so I've to write an answer instead. I also had the same issue and opened it on Scaffolding github repository. Here's the response:
currently there is an issue with scaffolding, that it doesn't support
model classes outside of the current project properly.
As a workaround, you can add the model temporarily to your web project
and then move it to the BLL/ DAL projects after scaffolding.
Plus they also opened this issue as a bug, quoting:
Scaffolding fails if model class is in a dependency (project/ library)
of the project on which scaffolding is being run. #251
Project A has a dependeny on Project B. Project B has model class If
you try to run scaffolding on Project A by using model class from
Project B, it fails with the below error: No model type returned for
type:
Hence, as of RC2, this is a bug in scaffolding tooling.

EF detect migration error but database in actual state

I have two projects. First project is API that call database via Entity Framework. Second project is a desktop application that uses same classes. But when i try to select data with EF in desktop application i got the following exception:
An exception of type 'System.InvalidOperationException'
occurred in EntityFramework.dll but was not handled in user code
Additional information: The model backing the '<modelName>
' context has changed since the database was created.
Consider using Code First Migrations to update the database
(http://go.microsoft.com/fwlink/?LinkId=238269).
But API project works fine. I use codefirst database migrations.
I've cleaned solution, make sure that all migration was applied to appropriate database and entities in correct state.
i have Entity Framework 6.0.0.0
You have options:
First the error to be ignored by adding Database.SetInitializer<API.Repository.MyDatabase.MyContext>(null); but using your context definition to the start-up code of you desktop application.
Check your database to determine what the cause of your error is; the migrations use a table called __MigrationHistory to check the status of the database against your code. The last row should be the same as our last migration. If they are different then you need to review you migration.
If you don't have any data you need to retain then revert all migrations and run Add-migration and Update-database.

Not able to instantiate a context from all project within a solution

I have a solution where there are following projects:
a context and entities developed under code-first approach.
A class library that present some business objects. the objets are using the EF context and its object for a DataAccess needs.
The solution has unit test of the business objects
I am able to execute my unit test successfully.
Now I have added a MVC 5 project where I would like to inject my business object. I have created a controller and view and put the same peace of code which has been passed in one of my unit test and also added the project web config proper entity framework and connnection strings sections. When I am executing the code I am facing the follwing error:
Migrations is enabled for context 'DatabaseContext' but the database
does not exist or contains no mapped tables. Use Migrations to create
the database and its tables, for example by running the
'Update-Database' command from the Package Manager Console.
When I am debugging my code I see that I am getting throug theese instructions
Database.SetInitializer(new ModelContextStrategy());
and
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
but still my result is negative, although everething works fine in my unit testss...
in case I am doing the unit tests first the db is get created and all unit test are through, and then I am starting the MVC application I am getting:
The model backing the 'DatabaseContext' context has changed since the
database was created. Consider using Code First Migrations to update
the database
What I am missing or doing wrong? (specific nuget packages installation for the mvc project? )
the problem was solved as soon a I enabled the entity framework for my MVC project
Install-Package EntityFramework -Version 6.1.0

Categories

Resources