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.SqlServerQueryCompilationContextFactory8无法翻译LINQ表达式“{__countries_1 =>包含(转换((?c2.Country.Id?))}”,并将在本地计算。要配置此警告,请使用DbContextOptionsBuilder.ConfigureWarnings API (事件id DbContextOptionsBuilder.ConfigureWarnings)ConfigureWarnings可用于重写DbContext.OnConfiguring方法或在应用程序服务提供程序上使用AddDbContext。
发布于 2017-09-21 03:44:58
我通过在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
https://stackoverflow.com/questions/45789512
复制相似问题