【问题标题】:EntityFrameworkCore Linq error: variable 'country.Country' of type 'Country' referenced from scope '', but it is not definedEntityFrameworkCore Linq 错误:从范围“”引用的“国家”类型的变量“国家.Country”,但未定义
【发布时间】:2017-08-21 04:53:44
【问题描述】:

EntityFrameworkCore:1.1.0

我总是通过“简单”的 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();

并且总是得到以下异常:

变量 'c2.Country' 类型为 '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。

【问题讨论】:

    标签: linq where invalidoperationexception


    【解决方案1】:

    我通过在 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多