EF核心多对多配置无法与Fluent

EF核心多对多配置无法与Fluent

本文介绍了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:

我的本意。我有一个有很多工作的客户。我应该能够将所有工作链接到该客户端。 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);
}

在此处了解更多信息:

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

09-05 03:08