【发布时间】:2017-05-11 22:30:52
【问题描述】:
给定以下具有继承的模型:
public class Credential { ... }
public class LocationCredential: Credential {
...
public long LocationId {get; set;}
public Location Location {get; set;}
}
public class EducationCredential: Credential {
...
public long SchoolId {get; set;}
public Location School {get; set;}
}
public class School { ... }
public class Location { ... }
实体框架核心仅创建一个带有Discriminator 列的Credentials 表来标识数据库中的继承类。
查询凭据时如何包含依赖实体? 我想做这样的事情:
var cred = await context.Credentials
.Where( c => c.CredentialId == 123)
.Include(c => c.Location) // cannot do these beacause they are not
.Include(c => c.School) // properties of Credential class
.FirstOrDefaultAsync();
我不想对每个继承的类进行单独的查询,然后返回非空类。不想要这些:
var cred = await context.EducationCredentials
.Where( c => c.CredentialId == 123)
.Include(c => c.School)
.FirstOrDefaultAsync();
if (cred == null) {
// try same thing with LocationCredentials
}
...
【问题讨论】:
标签: entity-framework linq asp.net-core entity-framework-core