[英]How to correctly use and reference Entity Framework Core in another project?
So I am building a REST API in .Net core with the following architecture and I'm wondering how to reference Entity Framework Core.因此,我正在使用以下架构在 .Net 核心中构建 REST API,我想知道如何引用 Entity Framework Core。
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.所有的查询都存储在 DAL(数据层)中,所以我在这里定义了 DbContext。
But at the same time, I also need to add DbContext to the ConfigureServices on WebAPI Startup ( services.AddDbContext<MyDBContext>();
).但同时,我还需要在 WebAPI 启动时将 DbContext 添加到 ConfigureServices ( services.AddDbContext<MyDBContext>();
)。
So the main issue is, is that I cannot access DbContext in WebAPI, because WebAPI doesn't directly reference DAL.所以主要问题是,我无法在 WebAPI 中访问 DbContext,因为 WebAPI 不直接引用 DAL。
I would appreciate very much any tips, suggestions or workarounds, that wouldn't impact the architecture in any major ways.我非常感谢任何不会以任何主要方式影响架构的提示、建议或解决方法。
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.添加 DAL 项目作为 WebApi 项目的引用,从那里你可以访问 DbContext,从 WebApi 访问 DAL 的查询需要使用存储库模式。 Here is a nice medium article on how to build a multi layer Asp.Net Core project这是一篇关于如何构建多层 Asp.Net Core 项目的不错的中等文章
https://medium.com/swlh/building-a-nice-multi-layer-net-core-3-api-c68a9ef16368 https://medium.com/swlh/building-a-nice-multi-layer-net-core-3-api-c68a9ef16368
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.