EF detect migration error but database in actual state - c#

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.

Related

Entity Framework Change Assembly Model not recognised

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);

Entity Framework code first migration throws exception

I have an asp.net MVC application using entity framework code first in which I have 3 projects. One is a class library project in which I put my Domain objects and DataContext class. In another project I put my admin MVC project, and the third project is the front end MVC application. I created a reference to my Domain project in my MVC application.
I enabled code first migration on my Domain object class library project and added first migrations initialCreate. Then I executed Update-database command. The database is created and my admin MVC application is able to access the data and everything is good. But when I rebuild my Domain object class library project the MVC application throws an exception as follows:
An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code
Additional information: The model backing the 'BookManyContext'
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 actually I won't touch my domain object nor context class. Even my MVC application won't access the data, and throws the exception.
What's wrong with my approach?
Do I need to put my Context class in a separate project?

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

Entity Framework setup issue

When I run my code
using (foodEntities db_Linq = new foodEntities())
{
return db_Linq.Database.Exists();
//return db_Linq.Foods.Any();
}
I keep getting this error in the output window.
A first chance exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll
What could be the issue? DB Credentials in web.config?
Other info:
I actually have 2 projects in my solution. One's a class library, the other MVC 4 web project.
Class library uses Entity Framework connection. Web project uses ADO connection string.
The above issue happens when web project calls the class library.
The code given above is from class library.
What version of the EF are you using? It seemed to me that you use EF6 and you have issues with the namespaces.
Please, check Updating Applications to use EF6 link. The main part for you is "Update namespaces for any core EF types being used"

How does automatic migrations work internally in Entity Framework 4.3

I want to know that how does automatic migrations work internally in Entity Framework 4.3 ?
i.e. when i add a new property in my Model and Run the ‘Update-Database’ command in Package Manager Console then Code First Migrations will update my database and will include the new column(for the new property).
Now i want to know how does 'Update-Database' command internally works ?
What do you want to know exactly?
If you run "Update-Database" and automatic migration is enabled for your DbContext, EF core compares current model with current Database schema and updates it if it would't cause any data loss. Otherwise you should specify -force key.

Categories

Resources