【问题标题】:A specified Include path is not valid指定的包含路径无效
【发布时间】:2012-04-02 23:57:17
【问题描述】:

使用 .Include - A specified Include path is not valid. The EntityType 'myProject.DAL.Paint' does not declare a navigation property with the name 'Color'. 时出现错误

DAL

public DBSet<Palete> Paletes {get; set; }
public DbSet<Paint> Paints { get; set; }

(注:modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;();

型号

public class Palete
{
  public virtual Paint Paint { get; set; }
}

public class Paint
{
    public string Color { get; set; }
}

query = query.Include(pal =&gt; pal.Paint.Color);

我该如何解决这个错误?

【问题讨论】:

  • 请注意,正确的拼写是palette...

标签: c# entity-framework include ef-code-first


【解决方案1】:

Color 是一个字符串属性 - 这里不需要 Include,因为 Color 不引用单独的实体。

鉴于更新只是在做

query = query.Include(pal => pal.Paint);

应该有效 - 如果您正在查询 Pallete 实体。

【讨论】:

  • include 引用实际上是嵌套的,我将提供一个更详细的示例。
  • 为什么只包括颜色不起作用(假设油漆有多个其他字段)?
  • Include 查询仅适用于完整实体 - 如果您想将查询限制为部分数据,您可以通过使用带有 Select 的投影来进一步限制结果
  • 哦,那太好了 :) 再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多