Not able to instantiate a context from all project within a solution - c#

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

Related

Circular dependency when generating dbcontext migrations into another assembly

As the name suggests I have multiple projects, and one of the project contains the dbcontext and all the models. So, I want to generate migrations from that project and put all generated migrations into another assembly (because I have multiple db providers for the same dbcontext, and one way is to move all migrations in different assemblies, so, i dont have problems later.).
Project containing the dbcontext and other stuff is not my main project, is just a class library.
The structure of the solution looks like below:
MyProject.DbContext
MyProject.DbContext.Migrations
MyProject
So, I reference the project MyProject.DbContext.Migrations from MyProject.DbContext to make the dotnet ef ... work, and I run this command:
dotnet ef migrations add InitMySql --project ..\MyProject.DbContext.Migrations
This will work fine as long as I do sth like this:
optionsBuilder.UseLazyLoadingProxies().UseMySql(configuration.ConnectionString, ServerVersion.AutoDetect(configuration.ConnectionString), builder =>
{
builder.CommandTimeout(configuration.CommandTimeout);
builder.UseNewtonsoftJson();
builder.MigrationsAssembly("MyProject.DbContext.Migrations");
});
But, now I have a big problem, the project MyProject.DbContext.Migrations needs also reference to MyProject.DbContext because of the new class created 'MyDbContextModelSnapshot' which has an attribute at the top: [DbContext(typeof(MyDbContext))] etc. Now I run into a circular dependency issue!
How is this supposed to be solved ?
I am currently using Asp.Net core 5, but I think this will happen for any version, seems more like a design limitation...

Using a separate migration project for F# data layer

I'm trying to setup migrations for my EF Core project on F# .NET 4.7.2. Currently I have the following data context:
type MyContext =
inherit DbContext
new() = { inherit DbContext() }
new(options: DbContextOptions<MyContext>) = { inherit DbContext(options) }
[<DefaultValue>]
val mutable apples: DbSet<AppleEntity>
member x.Apples
with get () = x.apples
and set v = x.apples <- v
Setting up my initial migration using Add-Migration Initial -Context Application.MyContext told me that F# is not a supported language for generating migrations, which is why I set up a C# project with a reference to the F# project.
However, when running the add migration command from this project, EF Core cannot find my context class: No DbContext named 'Application.MyContext' was found.
On Microsofts documentation about using a separate migration project, the following tip is stated:
If you have no existing migrations, generate one in the project containing the DbContext then move it. This is important because if the migrations assembly does not contain an existing migration, the Add-Migration command will be unable to find the DbContext.
But this is not a possibility for me, given that the context is in an F# project. Anyone have a clue on where to go from here?
EF is not F# friendly and AFAIK it's not in their near plan to improve the support for other languages. If you need migrations you have to use another framework like DbUp or another ORM like Linq2DB, Dapper. Easier option is to define the data layer in C# and reference it from other F# project
I managed to work around the issue by creating a C# version of MyContext and running the Add-Migration command. Then I deleted it and changed the reference in both MyContextModelSnapshot.cs and Initial.designer.cs to my F# data context.

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?

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.

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

Categories

Resources