EF 核心多对多配置不适用于 Fluent API

编程入门 行业动态 更新时间:2024-10-22 09:39:07
本文介绍了EF 核心多对多配置不适用于 Fluent API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在使用 Fluent API 和 EF Core 理解和实现多对多响应时遇到问题.

I am having trouble with understanding and implementing a many to many repationship using the FLuent API and EF Core.

我看过这个 问题并完全按照那样建立我的关系,但我收到以下错误:

I have looked at this question and set up my relationship exactly as that but I am getting the following error:

错误 CS1061CollectionNavigationBuilder"不包含WithMany"的定义,并且找不到接受CollectionNavigationBuilder"类型的第一个参数的扩展方法WithMany"(您是否缺少 using 指令或程序集引用?)

Error CS1061 'CollectionNavigationBuilder' does not contain a definition for 'WithMany' and no extension method 'WithMany' accepting a first argument of type 'CollectionNavigationBuilder' could be found (are you missing a using directive or an assembly reference?)

这是我的意图.我有一个客户,他有很多工作.我应该能够获得与该客户相关的所有工作.EF 应该在后台创建连接表...

This is my intention. I have a client who has many jobs. I should be able to get all the jobs linked to that client. EF should create the join table in the background...

这是我的课程:

public class Client : IEntityBase { public int Id { get; set; } public int? JobId { get; set; } public ICollection<Job> Jobs { get; set; } } public class Job : IEntityBase { public int Id { get; set; } } //my interface public interface IEntityBase { int Id { get; set; } }

编辑 这是我尝试过的 Fluent API 以及在.withMany"上出现错误的地方

EDIT Here is the Fluent API I tried and where I get the error on the ".withMany"

modelBuilder.Entity<Client>() .HasMany(p => p.Jobs) .WithMany(p => p.clients) .Map(m => { m.MapLeftKey("ClientId"); m.MapRightKey("JobId"); m.ToTable("ClientJob"); });

根据 Chris Sakell 的博客,我正在使用通用存储库模式.这是检索客户端的代码:

I am using a generic repository pattern as per Chris Sakell's blog. Here is the code for retrieving clients:

IEnumerable<Client> _clients = _clientRepository .AllIncluding(s => s.Creator, s => s.Jobs, s => s.State) .OrderBy(s => s.Id) .Skip((currentPage - 1) * currentPageSize) .Take(currentPageSize) .ToList();

我正在使用通用代码:

public virtual IEnumerable<T> AllIncluding(params Expression<Func<T, object>>[] includeProperties) { IQueryable<T> query = _context.Set<T>(); foreach (var includeProperty in includeProperties) { query = query.Include(includeProperty); } return query.AsEnumerable(); }

如何配置它以便我也可以根据上面的 Allinclude 语句使用 include 属性检索作业?

How do I configure this so I can retrieve the jobs as well using the includeproperty as per the Allincluding statement above?

推荐答案

您尝试实现的 Fluent API 示例来自 EF 6.EF Core 中的多对多关系配置略有不同.首先,您需要包含一个实体来表示连接/桥接表:

The Fluent API example you are trying to implement comes from EF 6. Many-to-Many relationship configuration is a little different in EF Core. For a start, you need to include an entity to represent the join/bridging table:

public class ClientsJobs { public int ClientId { get; set; } public int JobId { get; set; } public Client Client { get; set; } public Job Job { get; set; } }

然后你在 OnModelCreating 方法中像这样配置它:

Then you configure it like this in the OnModelCreating method:

protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<ClientsJobs>() .HasKey(x => new { x.ClientId, x.JobId }); modelBuilder.Entity<ClientsJobs>() .HasOne(x => x.Client) .WithMany(y => y.Jobs) .HasForeignKey(y => y.JobId); modelBuilder.Entity<ClientsJobs>() .HasOne(x => x.Job) .WithMany(y => y.Clients) .HasForeignKey(y => y.ClientId); }

在此处查看更多相关信息:www.learnentityframeworkcore/configuration/多对多关系配置

See more about it here: www.learnentityframeworkcore/configuration/many-to-many-relationship-configuration

注意:您确实需要在相关类中包含关系两端的导航属性,因此您需要将 Clients 属性添加到您的 作业实体.

Note: you do need to include navigational properties for both ends of the relationship in the related classes, so you need to add a Clients property to your Job entity.

更多推荐

EF 核心多对多配置不适用于 Fluent API

本文发布于:2023-11-17 04:44:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608873.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不适用于   核心   EF   Fluent   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!