本文介绍了实体框架核心2.1在左联接上引发ArgumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近将项目从.NET Core 2.0升级到2.1。我使用Entity Framework Core,并具有几个包含左联接的LINQ表达式。下面的代码是一个示例:

I recently upgraded my project from .NET Core 2.0 to 2.1. I use Entity Framework Core and have several LINQ expressions that include left joins. The following code is an example:

var tickets = (from ticket in dbContext.Tickets
               join member in dbContext.Members on ticket.MemberID equals member.ID into memberLEFT
               from member in memberLEFT.DefaultIfEmpty()
               join memberType in dbContext.MemberTypes on member.MemberTypeID equals memberType.ID into memberTypeLEFT
               from memberType in memberTypeLEFT.DefaultIfEmpty()
               select memberType)
              .ToList();

如果是.NET Core 2.0,则该行无例外地执行。在.NET Core 2.1中,它引发以下ArgumentException:

If .NET Core 2.0, this line executed with no exceptions. In .NET Core 2.1, it's throwing the following ArgumentException:

如果我更改LINQ以执行内部联接而不是左联接,则它不会执行任何问题。但是,该联接需要保留为左联接,所以我不能只更新所有代码以进行内部联接。

If I change the LINQ to perform inner joins instead of left, then it executes no problem. The joins need to be left joins though, so I can't just update all of my code to do inner joins.

有人知道为什么EF Core 2.1会抛出异常吗?

Does anyone know why EF Core 2.1 is now throwing ArgumentExecptions when performing left joins, and how to get around the issue?

推荐答案

我不确定为什么要在创建左联接时使用ArgumentExecptions以及如何解决该问题?您自己使用,而不是使用EF奖励模式。我以您的示例为例,对其进行了一些更改。

I'm not sure why you creating your joins on your own instead of using the EF releationship model. I've taken your example and changed it a little bit.

型号:

public class Ticket
{
    [Key]
    public int ID { get; set; }

    [Required]
    [StringLength(200)]
    public string Description { get; set; }

    public int? MemberID { get; set; }

    [ForeignKey(nameof(MemberID))]
    public Member Member { get; set; }
}

public class Member
{
    [Key]
    public int ID { get; set; }

    [Required]
    [StringLength(200)]
    public string Description { get; set; }

    public int? MemberTypeID { get; set; }

    public ICollection<Ticket> Tickets { get; set; }
    [ForeignKey(nameof(MemberTypeID))]
    public MemberType MemberType { get; set; }
}

public class MemberType
{
    [Key]
    public int ID { get; set; }

    [Required]
    [StringLength(200)]
    public string Description { get; set; }
}

要定义外部联接,只需使外键可为空即可。

To define an outer join just make the foreign-key nullable.

创建这样的模型后,您只需在查询中包含属性即可。

When you've created your models like this you can just include the properties in your query:

Tickets = (from t in _dbContext.Tickets
              .Include(t => t.Member)
                  .ThenInclude(m => m.MemberType)
            select t).ToList();

生成的sql如下:

SELECT [t].[ID], [t].[Description], [t].[MemberID], [t.Member].[ID], [t.Member]. 
    [Description], [t.Member].[MemberTypeID], [t.Member.MemberType].[ID], 
    [t.Member.MemberType].[Description]
FROM [Tickets] AS [t]
LEFT JOIN [Members] AS [t.Member] ON [t].[MemberID] = [t.Member].[ID]
LEFT JOIN [MemberTypes] AS [t.Member.MemberType] ON [t.Member].[MemberTypeID] = 
    [t.Member.MemberType].[ID]

希望能帮助您解决问题。

Hope that helps your issue.

这篇关于实体框架核心2.1在左联接上引发ArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 11:45