2017-11-06 9 views
2

私はEntity Frameworkで最初のアプリケーションを作成していますが、紛失しました。私は同様のスレッドからのアドバイスに従いましたが、成功しませんでした。Entity Frameworkを使用したLinq拡張メソッドの初心者間違い

私は私のサービスメソッド(だけですべてのデータを取得)で次のコマンドを実行します:。

using (WeatherStationDbContext entityContext = new WeatherStationDbContext()) 
      { 
       var weatherData = entityContext.Weather 
               .Include(x => x.Sensor) 
               .Include(x => x.Position.Select(p => p.Location)) 
               .ToList(); 

(...) 

と.INCLUDE(X => x.Positionでを選択した(p => P。所在地))私はエラーを取得: 「位置」「を選択」と何の拡張メソッドが受諾「を選択していない」の定義が含まれていませんが...

私は例えばのためのコードを追いましたhttps://msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx

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

私のクラス:DbContextで

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); 

} 
+1

あなたは多分https://docs.microsoft.com/en-us/ef/core/querying/related-data([ 'ThenInclude']を探しています)?私がリンクしている記事は、EF7/"EF Core"の記事です。 – Scott

+0

SQLサーバー側で適切な外部キーの関係が定義されていますか? – code4life

答えて

1

ただ、次のようにあなたのLINQを変更します。

var weatherData = entityContext.Weather 
           .Include(x => x.Sensor) 
           .Include(x => x.Position.Location) 
           .ToList(); 

あなたは問題がある発生した理由そのポジションイオンはコレクションではありません。 DbExtensions.Include MethodのためのMSDNドキュメントに詳述されているように:

Remarks

The path expression must be composed of simple property access expressions together with calls to Select in order to compose additional includes after including a collection property. Examples of possible include paths are:

  • To include a reference and then a reference one level down: query.Include(e => e.Level1Reference.Level2Reference) .

  • To include a collection and then a reference one level down: query.Include(e => e.Level1Collection.Select(l1 => l1.Level2Reference)) .

+2

イエス、ありがとう!なぜ私はSelect()を使う必要がないのか、msdn(私のポストのリンク)に掲示されている例のようにできない理由を説明できますか? – cantdoanything33

+1

@ cantdoanything33問題はありません。 – CalC

関連する問題