首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EntityFrameworkCore Linq错误:类型为“Country”的变量“country.Country”从作用域引用,但未定义

EntityFrameworkCore Linq错误:类型为“Country”的变量“country.Country”从作用域引用,但未定义
EN

Stack Overflow用户
提问于 2017-08-21 04:53:44
回答 1查看 28关注 0票数 0

EntityFrameworkCore: 1.1.0

我总是会得到一个带有“简单”linq查询的InvalidOperationException,并且不知道原因。它对标签很有效,但对国家不起作用。

我尝试执行以下查询:

代码语言:javascript
复制
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”,从范围引用,但未定义为

与下列实体:

代码语言:javascript
复制
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.SqlServerQueryCompilationContextFactory8无法翻译LINQ表达式“{__countries_1 =>包含(转换((?c2.Country.Id?))}”,并将在本地计算。要配置此警告,请使用DbContextOptionsBuilder.ConfigureWarnings API (事件id DbContextOptionsBuilder.ConfigureWarnings)ConfigureWarnings可用于重写DbContext.OnConfiguring方法或在应用程序服务提供程序上使用AddDbContext。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-21 03:44:58

我通过在ArticleCountry.Country属性中添加Required来解决这个问题

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45789512

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档