私は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);
}
あなたは多分https://docs.microsoft.com/en-us/ef/core/querying/related-data([ 'ThenInclude']を探しています)?私がリンクしている記事は、EF7/"EF Core"の記事です。 – Scott
SQLサーバー側で適切な外部キーの関係が定義されていますか? – code4life