C#
C#

help

Root Question Message

Shinyshark
Shinyshark11/19/2022
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?
Pobiega
Pobiega11/19/2022
are you using the dotnet ef commands, or the nuget package manager powershell commands like Add-Migration?
Shinyshark
Shinyshark11/19/2022
The second one.
Shinyshark
Shinyshark11/19/2022
Shinyshark
Shinyshark11/19/2022
This is the project structure if that helps.
Pobiega
Pobiega11/19/2022
okay, then you need to set your api project to your solution startup project
Pobiega
Pobiega11/19/2022
and then you set the dropdown in the nuget package manager window to your DAL project
Shinyshark
Shinyshark11/19/2022
I have multiple startup projects. Is that the issue? The API and Client should run at the same time.
Pobiega
Pobiega11/19/2022
dunno, may or may not work.
Shinyshark
Shinyshark11/19/2022
Oh haha that did work.
Shinyshark
Shinyshark11/19/2022
Can't believe it was so simple.
Pobiega
Pobiega11/19/2022
well, the ef tools need to know where to get your setup from (startup) and what your "target" project is 🙂
Shinyshark
Shinyshark11/19/2022
Yeah, I suppose it gets confused when you have multiple startup projects.
Pobiega
Pobiega11/19/2022
yup
Pobiega
Pobiega11/19/2022
that sounds likely
Shinyshark
Shinyshark11/19/2022
I don't know much about the internal workings of it.
Shinyshark
Shinyshark11/19/2022
but I know you don't usually have multiple startups.
Pobiega
Pobiega11/19/2022
you can specify the startup project with a command line option too, if you want
Pobiega
Pobiega11/19/2022
so you dont need to mess with the solution
Pobiega
Pobiega11/19/2022
I think its just --startupProject
Pobiega
Pobiega11/19/2022
but not 100% sure
Shinyshark
Shinyshark11/19/2022
Okay, maybe I just have to change the startup project each time I want to make a migration.
Shinyshark
Shinyshark11/19/2022
I imagine --startupProject would just do that for me once.
Pobiega
Pobiega11/19/2022
yep
Pobiega
Pobiega11/19/2022
or well, it would "set" it for the migration
Pobiega
Pobiega11/19/2022
but not change anything
Shinyshark
Shinyshark11/19/2022
Oh that's convenient.
Shinyshark
Shinyshark11/19/2022
Just temporarily then.
Pobiega
Pobiega11/19/2022
but you'd need to specify it each time you made a migration 😛
Pobiega
Pobiega11/19/2022
or Update-Database etc
Shinyshark
Shinyshark11/19/2022
Shinyshark
Shinyshark11/19/2022
I need this for my project
Shinyshark
Shinyshark11/19/2022
so I guess I would have to do that.
Shinyshark
Shinyshark11/19/2022
I'll document it properly for other people working on it.
Shinyshark
Shinyshark11/19/2022
It can't find --startupProject. Would I have to do something like this?
Add-Migration Initial -Project DAL --startupProject API
Pobiega
Pobiega11/19/2022
try -StartupProject API
Pobiega
Pobiega11/19/2022
i think the doubledash one is for dotnet ef
Shinyshark
Shinyshark11/19/2022
Hey that worked! Thank you very much
Shinyshark
Shinyshark11/19/2022
:AidenSmile:
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy