gpt4 book ai didi

c# - Linq Extension Methods with Entity Framework 初学者错误

转载 作者:太空宇宙 更新时间:2023-11-03 12:21:10 26 4
gpt4 key购买 nike

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

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

 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.Select(p => p.Location)) 中出现错误:“位置”不包含“选择”的定义,也没有扩展方法“选择”接受...

我遵循了 for e.g. 的代码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();

我的类(class):

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

在 DbContext 中:

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

}

最佳答案

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

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

您遇到问题的原因是 Position 不是集合。如 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)).

关于c# - Linq Extension Methods with Entity Framework 初学者错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47146021/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com