2016-11-28 2 views
0

私はこのような多くの関係1と2つのエンティティがあります。DbContextさ_context、次の2つのクエリがで終わった場合クエリに親と子を含めますか?

お客様1 ...... *予約

public class Customer 
    { 
     [Key] 
     public int Id { get; set; } 
     public string Name { get; set; } 
     public List<Booking> Bookings { get; set; } 
    } 


    public class Booking 
    { 
     [Key] 
     public int Id { get; set; } 
     public string Title{ get; set; } 
     public DateTime BookingDate { get; set; } 
     public int? CustomerId { get; set; } 
     public Customer Customer { get; set; }; 

    } 

を循環依存性。私たちは、エンティティA Bのクエリではその逆を含める必要があれば、我々はこれらの相互依存の関係を照会することができますどのように

_context.Customers.Include(x=> x.Bookings).ToList(); 

OR

_context.Bookings.Include(x=> x.Customer).ToList(); 

質問ありますか?

答えて

0

System.ComponentModel.DataAnnotations.Schema名前空間で定義されたForeignKey属性を使用できます。

create database ParentChild 
go 

use ParentChild 
go 

create table Customer 
(
    Id int not null identity(1, 1), 
    Name varchar(50) not null 
) 

create table Booking 
(
    Id int not null identity(1, 1), 
    Title varchar(50) not null, 
    BookingDate datetime not null, 
    CustomerId int not null 
) 
go 

insert into Customer values ('Parent') 
go 

insert into Booking values ('First booking', getdate(), 1) 
go 

出力結果:

お客様:親、予約:1

+0

は仮想キーワードで試しましたが、クエリには何の違いもありません。コンベンションベースのアプローチはEFドキュメントのように機能するので、流暢なAPIを使用しないでください。リレーショナル・エンティティーの例と、リレーショナル・エンティティーを照会して1つの照会に含める方法がありますか。 – Nexus23

+0

私は私の答えを更新しました。それを確認してください。 –

+0

あなたは質問に答えていません。仮想キー属性と外部キー属性を追加しても違いはありません。このサンプルをEFコアチームがチェックしてください。 https://github.com/aspnet/EntityFramework.Docs/blob/master/samples/core/Querying/Querying/RelatedData/Sample.cs 彼らは仮想または属性を使用していませんが、規則は使用していません。 – Nexus23

0

一つの可能​​な解決策

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 
using System.Linq; 
using Microsoft.EntityFrameworkCore; 

namespace ParentChild 
{ 
    public class Customer 
    { 
     [Key] 
     public int? Id { get; set; } 
     public string Name { get; set; } 
     public virtual List<Booking> Bookings { get; set; } 
    } 

    public class Booking 
    { 
     [Key] 
     public int? Id { get; set; } 
     public string Title { get; set; } 
     public DateTime? BookingDate { get; set; } 
     public int? CustomerId { get; set; } 
     [ForeignKey("CustomerId")] 
     public virtual Customer Customer { get; set; } 
    } 

    public class ParentChildDbContext : Microsoft.EntityFrameworkCore.DbContext 
    { 
     public ParentChildDbContext(String connectionString) 
     { 
      ConnectionString = connectionString; 
     } 

     public String ConnectionString { get; } 

     protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
     { 
      optionsBuilder.UseSqlServer(ConnectionString); 

      base.OnConfiguring(optionsBuilder); 
     } 

     public DbSet<Customer> Customer { get; set; } 

     public DbSet<Booking> Booking { get; set; } 
    } 

    public class Program 
    { 
     public static void Main(string[] args) 
     { 
      var dbContext = new ParentChildDbContext("server=(local);database=ParentChild;integrated security=yes;"); 

      var data = dbContext.Customer.Include(p => p.Bookings).ToList(); 

      Console.WriteLine("Customer: {0}, Bookings: {1}", data.First().Name, data.First().Bookings.Count); 

      Console.ReadKey(); 
     } 
    } 
} 

また、私はこのテスト用のデータベースを作成しましたAsp.Net Coreを使用していた私の場合の上記の問題は、MVCオプションで参照ループを処理するように次のように伝えることでした:

services.AddMvc() 
       .AddJsonOptions(options => 
       { 
        options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; 
       }); 

私は、参照ループは、消費者の側で処理されるべきであるか、EFコアによって処理されるべきかどう答えるためにEFコアチームとissueを調達しています。応答を取得すると、回答を更新します。

関連する問題