ASP.NET MVC Multiple Projects migrating to One Database - c#

I was wondering if its possible to have multiple Entity Framework Code First Projects migrating onto a single Database? I've tried looking and found this link but I think the instructions were kind of vague, so I made a list:
Done (created a new class library)
Does the new class library have to reference my project? Isn't it the other way around?
What are model snapshot files? Do I move my models also?
Where is this located? Is it in the class library?
What is the startup assembly? Is it my initial project that was referenced by the class library?
Add-Migration NewMigration -Project MyApp.Migrations (What is MyApp.Migrations)

Related

How to manage multiple dbcontext in same project with EF Core Power Tools?

I need to manage multiple context in the same project with different connection strings, I'm using EF Core Power Tools, but this tool it's generating a file config for whole project, worth noting that I'm working with Database First and separeting the entities from dbcontext in another class library project.
Assign a unique name to the config file according to the pattern described here: https://github.com/ErikEJ/EFCorePowerTools/wiki/Reverse-Engineering#using-multiple-dbcontexts-in-the-same-project

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.

Can I share entities between solutions?

I have a problem with sharing entities in 2 different solutions.
I have one entity (Country) that I need in 2 solutions. One solution it's a web application and the other it's a console application but I need this entity in both.
Additionally, we are doing migrations and I don't know what the behaviour of code first is in this case if I change the entity twice one per solution.
It is possible to share entities between solutions in entity framework 6 code first?
If I understood correctly you want to use an entitie which is contained in a project and use it botjh in a web project and a console project. You can do that by referencing the output dll of your entities project from where you need it.
In your case you would add a reference to entities.dll on the consoleapplication project and another one on the webapplication project.
Personally I would have all the projects in the same solution and reference each project I needed. But it is possible to do it your way.

Create instance of a class in other project

I have a .Net solution with two projects. The first project it's a MVC4 application and the second one it's a ClassLibrary project.
I can access to the classes of my MVC4 Projet to my ClassLibrary (like models), but I can't access from my to my ClassLibrary to my MVC4 project to inject a dependency or create an instance.
I'm missing some reference?
Writing a class Library that will be used by an MVC project should be mostly self sufficient, (having dependencies on other libraries etc) but the Library really shouldn't need to know anything about the MVC project. If that is what you are trying to do, you need to do some redesigning. Models and other things should be not be a part of the MVC project adding them to the class library would be a better design.
There is no need to other Classlibrary. You can use all those features in the Model.
You can use any dependincy patterns in model like NUnity, Ninject etc. but EntityFramework is always offers Repository pattern as a dipendincy injection with IOC.
If you want to use instate of all you can create a sub project and refference it to the model and made your dependincy container control under there.

Categories

Resources