2016-05-22 1 views
0

Entity Framework 7のように見えますが、遅延読み込みはサポートされていませんが、実装されていません。 Include(...)方法。エンティティからのすべての仮想コレクションを返すEF7(RC2)の奇妙な問題Include

この場合、予想される動作に従う必要があるかどうかはわかりません。下の私の複製を参照してください。注:this github branch/commitはこれを再現します。

DbContextとリポジトリプロバイダ追加:

// In ConfigureServices 
services.AddDbContext<PouarfDbContext>(options => options.UseInMemoryDatabase()); 
services.AddScoped<IContactProvider, ContactProvider>(); 

シードアプリケーションデータ:

public async Task AddPerson(Person person) 
{ 
    await Task.Run(() => _dbContext.Add(person)); 
} 

public async Task<IEnumerable<Person>> GetPeople() 
{ 
    // Notice no Include(...) 
    return await _dbContext.People.ToListAsync(); 
} 

Personオブジェクト:

実装例からの

// In Configure 
// CreateSampleData() adds a bunch of `People` objects to a list, 
// and then loops through them, adding each one to the DbContext 
// through the `ContactProvider` 
Task.Run(async() => 
    await new 
    MockPeople(app 
     .ApplicationServices 
     .GetService<IContactProvider>()).CreateSampleData()); 

カップル

public class Person : ContactInformationBase // Contains Id 
{ 
    ... 

    public virtual ICollection<PhoneNumber> PhoneNumbers { get; set; } 
} 

そして、私のコントローラのアクションで、私はEFから人々を取得しています:

public async Task<IActionResult> Index() 
{ 
    // Contains a populated list of `PhoneNumbers`, even though 
    // they should only be included if I explicitly asked EF7 to load them. 
    var people = await _contactProvider.GetPeople(); 
    .. 
} 

だから、基本的に、EF7は彼らだけ含まれるべきにもかかわらず、私の主な目的からマッピングされたエンティティを提供しています私が明示的に質問すると、Include(p => p.PhoneNumbers)

私はEntity Frameworkをして、ASP.NETコア1 RC2を使用しています7

EDIT

はちょうど私のコントローラにAPIアクションを追加し、その結果は...奇妙です。

[Route("api/[action]")] 
public async Task<IEnumerable<Person>> People() 
{ 
    // True adds all the Includes off the DbSet as needed. 
    return await _contactProvider.GetPeople(true); 
} 

この呼び出しでは、マップされたエンティティは返されません。 Include()コールを削除または追加しても、結果には影響しません。コアPersonエンティティのみが返され、マッピングされたエンティティはすべてnullになります。

答えて

1

私がhereと指摘したように、問題は自分の責任でした。

この問題は、DbSetのIncludeパターンを利用するアプローチにありました。

私はこれを持っていた

WRONG

var people = _dbContext.People; 

if (includeRelationships) 
{ 
    people.Include(p => p.PhoneNumbers) 
     .Include(p => p.EmailAddresses) 
     .Include(p => p.StreetAddresses); 
} 

return await people.FirstOrDefaultAsync(p => p.Id.Equals(id)); 

そして、これにそれを変更し、実際には、ここにIncludeの適用気づくRIGHT

IQueryable<Person> people = _dbContext.People; 
if (includeRelationships) 
{ 
    people = people 
     .Include(p => p.EmailAddresses) 
     .Include(p => p.PhoneNumbers) 
     .Include(p => p.StreetAddresses); 
} 

return await people.FirstOrDefaultAsync(p => p.Id.Equals(id));