EF核心多对多配置无法与Fluent API一起使用

编程入门 行业动态 更新时间:2024-10-21 13:06:48
本文介绍了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:

错误CS1061'CollectionNavigationBuilder'不包含'WithMany'的定义,也没有扩展方法'WithMany'可以找到接受类型为'CollectionNavigationBuilder'的第一个参数(您是否缺少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();

,而我使用的通用代码如下:

and I am using the generic code as per:

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(); }

如何配置它,以便我也可以使用includeproperty检索作业

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/many-to-many-relationship-configuration

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

注意:您要做需要在相关类中包括关系两端的导航属性,因此您需要添加 Clients 属性分配给您的 Job 实体。

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:46:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608879.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:核心   EF   API   Fluent

发布评论

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

>www.elefans.com

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