How to correctly use and reference Entity Framework Core in another project?

.net-core asp.net-core c# entity-framework entity-framework-core

Question

So I am building a REST API in .Net core with the following architecture and I'm wondering how to reference Entity Framework Core.

enter image description here

These are three projects, referencing each other the way it's shown on the image. All the queries are stored in DAL (Data Layer), so this is where I defined DbContext.

But at the same time, I also need to add DbContext to the ConfigureServices on WebAPI Startup (services.AddDbContext<MyDBContext>();).

So the main issue is, is that I cannot access DbContext in WebAPI, because WebAPI doesn't directly reference DAL.

I would appreciate very much any tips, suggestions or workarounds, that wouldn't impact the architecture in any major ways.

1
0
3/23/2020 6:20:48 PM

Popular Answer

Add the DAL project as reference to the WebApi project, from there you can get access to DbContext, to access the queries of DAl from WebApi you need to used the repository pattern. Here is a nice medium article on build a multi layer Asp.Net Core project

https://medium.com/swlh/building-a-nice-multi-layer-net-core-3-api-c68a9ef16368

2
3/23/2020 6:16:36 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow