C
C#•2y ago
Shinyshark

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