EntityFrameworkCore Linq错误:变量' country.Country'类型'国家'从范围''引用,但未定义

时间:2017-08-21 04:53:44

标签: linq where invalidoperationexception

EntityFrameworkCore:1.1.0

我总是得到一个带有"简单"的InvalidOperationException。 linq查询并无法弄清楚原因。它适用于标签,但不适用于各国。

我尝试执行以下查询:

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

并始终获得以下异常:

变量&c; 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表达式&#39; {__ countries_1 =&gt;含有(转换(([c2.Country] .ID))?)}&#39;无法翻译,将在本地进行评估。要配置此警告,请使用DbContextOptionsBuilder.ConfigureWarnings API(事件ID&#39; RelationalEventId.QueryClientEvaluationWarning&#39;)。重写DbContext.OnConfiguring方法或在应用程序服务提供程序上使用AddDbContext时,可以使用ConfigureWarnings。

1 个答案:

答案 0 :(得分:0)

我通过在ArticleCountry.Country属性

中添加[必需]来修复此问题
{ defer; panic(); recover() }

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