使用实体框架的Linq扩展方法中的初学者错误

时间:2017-11-06 21:21:52

标签: c# .net entity-framework linq

我正在使用Entity Framework编写我的第一个应用程序并迷路了。我遵循类似线程的建议但没有成功。

我想在我的服务方法中执行以下操作(只获取所有数据):

// Load all blogs, all related posts, and all related comments 
var blogs1 = context.Blogs 
                   .Include(b => b.Posts.Select(p => p.Comments)) 
                   .ToList(); 

在.Include(x => x.Position。 选择(p => p.Location)) 我收到错误消息: “位置”不包含“选择”的定义,也没有扩展方法“选择”接受...

我按照以下代码进行操作: https://msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx

public class Location
{
    public int Id { get; set; }

    public string Description { get; set; }

    public int Width { get; set; }

    public int Length { get; set; }
}

public class Position
{
    public int Id { get; set; }

    public int SensorId { get; set; }

    public virtual Sensor Sensor { get; set; }

    public int LocationId { get; set; }

    public virtual Location Location { get; set; }

    public string Description { get; set; }

    public float CoordinateX { get; set; }

    public float CoordinateY { get; set; }

}

public class Sensor
{
    public int Id { get; set; }

    public string Description { get; set; }
}

public class Weather
{
    public int Id { get; set; }

    public int SensorId { get; set; }

    public virtual Sensor Sensor { get; set; }

    public int PositionId { get; set; }

    public virtual Position Position { get; set; }

    public float Temperature { get; set; }

    public float Humidity { get; set; }

    public DateTime Date { get; set; }
}

我的课程:

public DbSet<Sensor> Sensor { get; set; }
public DbSet<Location> Location { get; set; }
public DbSet<Position> Position { get; set; }
public DbSet<Weather> Weather { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

    modelBuilder.Entity<Weather>()
                .HasRequired(c => c.Sensor)
                .WithMany()
                .WillCascadeOnDelete(false);

    modelBuilder.Entity<Weather>()
                .HasRequired(c => c.Position)
                .WithMany()
                .WillCascadeOnDelete(false);

}

在DbContext中:

course-------score 

A---------------4

B---------------6

C---------------3

A---------------2

B---------------4

C---------------4

1 个答案:

答案 0 :(得分:1)

只需将您的Linq更改为以下内容:

query.Include(e => e.Level1Reference.Level2Reference)

您遇到问题的原因是Position不是集合。详见DbExtensions.Include Method的MSDN文档:

  

<强>说明

     

路径表达式必须由简单的属性访问组成   表达式以及对选择的调用以便撰写   其他包括包括收集属性后。示例   可能包括路径:

     
      
  • 要包含引用,然后引用一个级别:   query.Include(e => e.Level1Collection.Select(l1 => l1.Level2Reference))

  •   
  • 要包含一个集合,然后在一个级别下添加一个参考:PS C:\Users\John Alvarez\Documents\School\Practice> ls Directory: C:\Users\John Alvarez\Documents\School\Practice Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 11/6/2017 2:16 PM 101 helloWorld.cpp PS C:\Users\John Alvarez\Documents\School\Practice> g++ -o cpp.exe .\helloWorld.cpp PS C:\Users\John Alvarez\Documents\School\Practice> ls Directory: C:\Users\John Alvarez\Documents\School\Practice Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 11/6/2017 2:16 PM 101 helloWorld.cpp PS C:\Users\John Alvarez\Documents\School\Practice> cpp.exe

  •