EF 核心多对多配置不适用于 Fluent API [英] EF core many to many configuration not working with Fluent API

查看:20
本文介绍了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);
}

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

See more about it here: http://www.learnentityframeworkcore.com/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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆