简体   繁体   English

如何在另一个项目中正确使用和引用 Entity Framework Core?

[英]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.

相关问题 通过另一个项目引用.NET Core中的实体框架6 - Reference Entity Framework 6 in .NET Core through another project 如何在Entity Framework Core中使用类似于参考表的表 - How to use a table like a reference table in Entity Framework Core 如何在一个解决方案中在另一个项目上使用实体框架安装 - How to use Entity framework install on another project in one solution 如何将 C# 8.0 可空引用类型与 Entity Framework Core 模型一起使用? - How to use C# 8.0 Nullable Reference Types with Entity Framework Core models? 如何在另一个项目中分离实体框架 - How to have entity framework saparated in another project 如何将LinqPad与Entity Framework Core一起使用? - How to use LinqPad with Entity Framework Core? 如何在实体框架和 EF Core 中使用 .Include() - How to use .Include() in Entity Framework and EF Core 如何在 Entity Framework Core 中使用抽象 class? - How to use abstract class in Entity Framework Core? 如何在 Entity Framework Core 中使用 ValueGeneratedOnUpdateSometimes - How to use ValueGeneratedOnUpdateSometimes in Entity Framework Core 我们如何在一个项目中添加实体框架,并在另一个项目中将其用作服务? - How can we add entity framework in one project and use it as a service in another project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM