は、このサンプルを取る:
public class RentContext : DbContext
{
public DbSet<Student> Students { get; set; }
public DbSet<Rent> Rents { get; set; }
public DbSet<Car> Cars { get; set; }
}
public class Car
{
public int Id { get; set; }
public string Model { get; set; }
public double Price { get; set; }
}
public class Rent
{
public int Id { get; set; }
public Student Student { get; set; }
public Car Car { get; set; }
}
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Year { get; set; }
}
家賃は学生や車が含まれています。
var rents = ctx.Rents
.Include(x => x.Student)
.Include(x => x.Car)
//.Include(x => x.Student)
.ToList();
これで生成されたSQL::
SELECT
[Extent1].[Id] AS [Id],
[Extent2].[Id] AS [Id1],
[Extent2].[Name] AS [Name],
[Extent2].[Year] AS [Year],
[Extent3].[Id] AS [Id2],
[Extent3].[Model] AS [Model],
[Extent3].[Price] AS [Price]
FROM [dbo].[Rents] AS [Extent1]
LEFT OUTER JOIN [dbo].[Students] AS [Extent2] ON [Extent1].[Student_Id] = [Extent2].[Id]
LEFT OUTER JOIN [dbo].[Cars] AS [Extent3] ON [Extent1].[Car_Id] = [Extent3].[Id]
のは、複製クエリがインクルードしてみましょう:
var rents = ctx.Rents
.Include(x => x.Student)
.Include(x => x.Car)
.Include(x => x.Student)
.ToList();
あなた」
はユニークな句を含めると、クエリを作ることができますこのSQLを取得します:
SELECT
[Extent1].[Id] AS [Id],
[Extent2].[Id] AS [Id1],
[Extent2].[Name] AS [Name],
[Extent2].[Year] AS [Year],
[Extent3].[Id] AS [Id2],
[Extent3].[Model] AS [Model],
[Extent3].[Price] AS [Price]
FROM [dbo].[Rents] AS [Extent1]
LEFT OUTER JOIN [dbo].[Students] AS [Extent2] ON [Extent1].[Student_Id] = [Extent2].[Id]
LEFT OUTER JOIN [dbo].[Cars] AS [Extent3] ON [Extent1].[Car_Id] = [Extent3].[Id]
EFは、インクルードを複数回指定しても同じSQLを生成できるほどスマートです。
UPDATE:繰り返し(何度も)が含ま
のは、これを試してみましょう:繰り返し
var rents = ctx.Rents
.Include(x => x.Student)
.Include(x => x.Car)
.Include(x => x.Car)
.Include(x => x.Car)
.Include(x => x.Car)
.Include(x => x.Car)
.Include(x => x.Car)
.Include(x => x.Car)
.Include(x => x.Car)
.Include(x => x.Car)
.Include(x => x.Car)
.Include(x => x.Car)
.Include(x => x.Car)
.Include(x => x.Car)
.Include(x => x.Car)
.Include(x => x.Car)
.Include(x => x.Car)
.Include(x => x.Car)
.Include(x => x.Car)
.Include(x => x.Car)
.Include(x => x.Car)
.Include(x => x.Car)
.Include(x => x.Car)
.Include(x => x.Car)
.Include(x => x.Car)
.Include(x => x.Student)
.Include(x => x.Student)
.Include(x => x.Student)
.Include(x => x.Student)
.Include(x => x.Student)
.Include(x => x.Student)
.Include(x => x.Student)
.Include(x => x.Student)
.Include(x => x.Student)
.Include(x => x.Student)
.Include(x => x.Student)
.Include(x => x.Student)
.Include(x => x.Student)
.ToList();
が含まれ、いくつかの回。そして、生成されたSQLは次のとおりです。
SELECT
[Extent1].[Id] AS [Id],
[Extent2].[Id] AS [Id1],
[Extent2].[Name] AS [Name],
[Extent2].[Year] AS [Year],
[Extent3].[Id] AS [Id2],
[Extent3].[Model] AS [Model],
[Extent3].[Price] AS [Price]
FROM [dbo].[Rents] AS [Extent1]
LEFT OUTER JOIN [dbo].[Students] AS [Extent2] ON [Extent1].[Student_Id] = [Extent2].[Id]
LEFT OUTER JOIN [dbo].[Cars] AS [Extent3] ON [Extent1].[Car_Id] = [Extent3].[Id]
もう一度同じコードを再度入力します。あ、はい。ちょっと変わったことはありますが、それは大丈夫でしょう。
希望すると便利です。
私はそこに問題はないはずだと思っていますが、あなたはそれを実際にそのように使うことになっています。私はその文書を見つけようとします。 – Zack
私の主張は、私は学生だけが必要だということです。基本エンティティは人であるため、1人の生徒がいます。 –