EntityFrameworkCore Linq error: variable 'country.Country' of type 'Country' referenced from scope '', but it is not defined

EntityFrameworkCore Linq error: variable 'country.Country' of type 'Country' referenced from scope '', but it is not defined

EntityFrameworkCore:1.1.0

我总是得到一个带有 "simple" linq 查询的 InvalidOperationException 并且无法弄清楚原因。它适用于标签,但不适用于国家/地区。

我尝试执行以下查询:

var tags = new string[]{"guid1", "guid2"};
var countries = new int[]{1, 2};
var test = _dbContext.Articles
            .Include(a => a.Countries).ThenInclude(c => c.Country)
            .Include(a => a.Tags)
            .Where(article => article.Tags.Any(t => tags.Contains(t.Tag.Id)) &&
                              article.Countries.Any(c2 => countries.Contains(c2.Country.Id)))
           .ToList();

并且总是得到以下异常:

'Country' 类型的变量 'c2.Country' 从范围 '' 引用,但未定义

具有以下实体:

public class Article
{
    /// <summary>
    /// Unique ID of the article
    /// </summary>
    [Required]
    public Guid Id { get; set; }

    /// <summary>
    /// Article type
    /// </summary>
    [Required]
    public ArticleTypes Type { get; set; }

    /// <summary>
    /// Gets or sets the article tags.
    /// </summary>
    /// <value>
    /// The article tags.
    /// </value>
    public ICollection<ArticleTag> Tags { get; set; }

    /// <summary>
    /// Gets or sets the countries.
    /// </summary>
    /// <value>
    /// The countries.
    /// </value>
    public ICollection<ArticleCountry> Countries { get; set; }
}

public class ArticleCountry
{
    /// <summary>
    /// Gets or sets the identifier.
    /// </summary>
    /// <value>
    /// The identifier.
    /// </value>
    public Guid Id { get; set; }

    /// <summary>
    /// Gets or sets the country.
    /// </summary>
    /// <value>
    /// The country.
    /// </value>
    public Country Country { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public int Position { get; set; }
}

public class Country
{
    /// <summary>
    /// Gets or sets the identifier.
    /// </summary>
    /// <value>
    /// The identifier.
    /// </value>
    [JsonIgnore]
    public int Id { get; set; }

    /// <summary>
    /// Unique country code
    /// </summary>
    [JsonProperty("key")]
    [Required]
    [StringLength(2)]
    public string Code { get; set; }

    /// <summary>
    /// Country name
    /// </summary>
    [JsonProperty("name")]
    [Required]
    public string Name { get; set; }
}

public class Tag
{
    /// <summary>
    /// Tag unique key
    /// </summary>
    [JsonProperty("key")]
    [Required]
    public Guid Id { get; set; }

    /// <summary>
    /// Tag name
    /// </summary>
    [JsonProperty("name")]
    [Required]
    public string Name { get; set; }
}

public class ArticleTag
{
    /// <summary>
    /// Gets or sets the identifier.
    /// </summary>
    /// <value>
    /// The identifier.
    /// </value>
    [Required]
    public Guid Id { get; set; }

    /// <summary>
    /// Gets or sets the order.
    /// </summary>
    /// <value>
    /// The order.
    /// </value>
    [Required]
    public int Position { get; set; }

    /// <summary>
    /// Gets or sets the tag.
    /// </summary>
    /// <value>
    /// The tag.
    /// </value>
    [Required]
    public Tag Tag { get; set; }
}

我还可以在控制台中看到以下警告:

警告:Microsoft.EntityFrameworkCore.Query.Internal.SqlServerQueryCompilationContextFactory[8] LINQ 表达式 '{__countries_1 => Contains(Convert((?[c2.Country].Id?)))}' 无法翻译,将在本地求值。要配置此警告,请使用 DbContextOptionsBuilder.ConfigureWarnings API(事件 ID 'RelationalEventId.QueryClientEvaluationWarning')。在覆盖 DbContext.OnConfiguring 方法或在应用程序服务提供商上使用 AddDbContext 时,可以使用 ConfigureWarnings。

我通过在 ArticleCountry.Country 属性

中添加 [Required] 解决了这个问题
public class ArticleCountry
{
   /// <summary>
   /// Gets or sets the identifier.
   /// </summary>
   /// <value>
   /// The identifier.
   /// </value>
   [Required]
   public Guid Id { get; set; }

   /// <summary>
   /// Gets or sets the country.
   /// </summary>
   /// <value>
   /// The country.
   /// </value>
   [Required]
   public Country Country { get; set; }

   /// <summary>
   /// 
   /// </summary>
   public int Position { get; set; }
}

似乎连接不喜欢可为空的 FK