Entity Framework code first migration throws exception - c#

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?

Related

Error comes up when creating controller for ASP.Net Web API

I have just created a ASP.NET web API project with Visual Studio 17 and I have created a Class Library to connect it to an SQL database I created. the problem is when I try to create a controller in the project, I get the error,
There was an error running the selected code generator: could not load file 'ClassLibrary.dll' or one of it's dependencies. this system could not find the file specified.
Does anyone what the problem could be. I have looked at the folder where the said file should be contained in the ClassLibrary and it is empty.
I gather that it might be from my Entity Framework, I am using ADO.Net Entity Data Model.
For ASP.NET web API project no need to add Class Library:
Just right click in Solution and add ADO.net Model
Configure with Database
Add required Database Tables
Build solution
First you have to add reference of your class library project if you don't then just give a built to your project in which you added a reference of class library project, then try to create a controller it will definitely help you....

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

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"

Categories

Resources