Unable to create an object of type 'UnitOfWork'. [Answered]
Hello everyone. I have two projects; one is an API and the other is a DAL (Data Access Layer). I want the DbContext and the Migrations to live in the DAL. However, it seems I have to add migrations via the API project.
How can I create migrations in the DAL project?
Pobiega11/19/2022
are you using the dotnet ef commands, or the nuget package manager powershell commands like Add-Migration?
Shinyshark11/19/2022
The second one.
Shinyshark11/19/2022
Shinyshark11/19/2022
This is the project structure if that helps.
Pobiega11/19/2022
okay, then you need to set your api project to your solution startup project
Pobiega11/19/2022
and then you set the dropdown in the nuget package manager window to your DAL project
Shinyshark11/19/2022
I have multiple startup projects. Is that the issue? The API and Client should run at the same time.
Pobiega11/19/2022
dunno, may or may not work.
Shinyshark11/19/2022
Oh haha that did work.
Shinyshark11/19/2022
Can't believe it was so simple.
Pobiega11/19/2022
well, the ef tools need to know where to get your setup from (startup) and what your "target" project is 🙂
Shinyshark11/19/2022
Yeah, I suppose it gets confused when you have multiple startup projects.
Pobiega11/19/2022
yup
Pobiega11/19/2022
that sounds likely
Shinyshark11/19/2022
I don't know much about the internal workings of it.
Shinyshark11/19/2022
but I know you don't usually have multiple startups.
Pobiega11/19/2022
you can specify the startup project with a command line option too, if you want
Pobiega11/19/2022
so you dont need to mess with the solution
Pobiega11/19/2022
I think its just --startupProject
Pobiega11/19/2022
but not 100% sure
Shinyshark11/19/2022
Okay, maybe I just have to change the startup project each time I want to make a migration.
Shinyshark11/19/2022
I imagine --startupProject would just do that for me once.
Pobiega11/19/2022
yep
Pobiega11/19/2022
or well, it would "set" it for the migration
Pobiega11/19/2022
but not change anything
Shinyshark11/19/2022
Oh that's convenient.
Shinyshark11/19/2022
Just temporarily then.
Pobiega11/19/2022
but you'd need to specify it each time you made a migration 😛
Pobiega11/19/2022
or Update-Database etc
Shinyshark11/19/2022
Shinyshark11/19/2022
I need this for my project
Shinyshark11/19/2022
so I guess I would have to do that.
Shinyshark11/19/2022
I'll document it properly for other people working on it.
Shinyshark11/19/2022
It can't find --startupProject. Would I have to do something like this?
Add-Migration Initial -Project DAL --startupProject API