表达式 'y.Cases' 在 'Include' 操作中无效
The expression 'y.Cases' is invalid inside an 'Include' operation
我有一对多关系数据库。 DbSet Companies
是“一”,DbSet Cases
是“多”。这是我的上下文和模型 classes:
数据库上下文
class CaseContext : DbContext
{
public DbSet<ParticipantCompany> Companies{ get; set; }
public DbSet<ParticipantPerson> Persons { get; set; }
public DbSet<LegalCase> Cases { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite("Data Source=Clients.db");
}
ParticipantCompany
class 继承自 Participant
Class.
public class ParticipantCompany : Participant
{
public ParticipantCompany():this(false, "","","","") { }
public ParticipantCompany (bool isclient) : this(isclient, "","","","") { }
public ParticipantCompany(bool isclient, string name, string address, string inncompany, string ogrn) : base(isclient, SubjectType.Company)
{
Name = name;
Address = address;
InnCompany = inncompany;
Ogrn = ogrn;
}
public string InnCompany { get; set; }
public string Ogrn { get; set; }
}
public abstract class Participant
{
public Participant(bool isclient, SubjectType Type, string name, string address)
{
SubjType = Type;
IsClient = isclient;
Name = name;
Address = address;
}
public Participant(bool isclient, SubjectType Type ) : this(isclient, Type, "","")
{
}
public int Id { get; set; }
public SubjectType SubjType { get; private set; }
public bool IsClient { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public List<LegalCase> Cases = new List<LegalCase>();
}
LegalCase
class代表关系中的“多”
public class LegalCase
{
public LegalCase() : this("", CaseType.ArbGeneral){}
public LegalCase(string casefabula, CaseType casetype)
{
CaseFabula = casefabula;
CaseType = casetype;
}
public int Id { get; set; }
public string CaseFabula { get; set; }
public CaseType CaseType { get; set; }
//public ICaseParticipant Client { get; set; }
public int? CompanyId { get; set; }
public ParticipantCompany Company { get; set; }
public int? PersonId { get; set; }
public ParticipantPerson Person { get; set; }
}
现在是查询:
using(var db = new CaseContext())
{
var QClients = db.Companies
.Where(x => x.IsClient == true)
//Exception: The expression 'y.Cases' is
// invalid inside an 'Include' operation,
// since it does not represent a property
// access: 't => t.MyProperty'. etc
.Include(y => y.Cases)
.ToList();
}
我试图将 y
显式转换为 ParticipantCompany
因为这是执行提示所暗示的:
To target navigations declared on derived types, use casting ('t => ((Derived)t).MyProperty') or the 'as' operator ('t => (t as Derived).MyProperty')
但它生成相同的异常:
using(var db = new CaseContext())
{
var QClients = db.Companies
.Where(x => x.IsClient == true)
//Same exception
.Include(y => (y as ParticipantCompany).Cases)
.ToList();
}
根据异常建议将 Cases
从字段更改为 属性:
public List<LegalCase> Cases { get; set; } = new List<LegalCase>();
来自docs:
Each entity type in your model has a set of properties, which EF Core will read and write from the database. If you're using a relational database, entity properties map to table columns.
By convention, all public properties with a getter and a setter will be included in the model.
我有一对多关系数据库。 DbSet Companies
是“一”,DbSet Cases
是“多”。这是我的上下文和模型 classes:
数据库上下文
class CaseContext : DbContext
{
public DbSet<ParticipantCompany> Companies{ get; set; }
public DbSet<ParticipantPerson> Persons { get; set; }
public DbSet<LegalCase> Cases { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite("Data Source=Clients.db");
}
ParticipantCompany
class 继承自 Participant
Class.
public class ParticipantCompany : Participant
{
public ParticipantCompany():this(false, "","","","") { }
public ParticipantCompany (bool isclient) : this(isclient, "","","","") { }
public ParticipantCompany(bool isclient, string name, string address, string inncompany, string ogrn) : base(isclient, SubjectType.Company)
{
Name = name;
Address = address;
InnCompany = inncompany;
Ogrn = ogrn;
}
public string InnCompany { get; set; }
public string Ogrn { get; set; }
}
public abstract class Participant
{
public Participant(bool isclient, SubjectType Type, string name, string address)
{
SubjType = Type;
IsClient = isclient;
Name = name;
Address = address;
}
public Participant(bool isclient, SubjectType Type ) : this(isclient, Type, "","")
{
}
public int Id { get; set; }
public SubjectType SubjType { get; private set; }
public bool IsClient { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public List<LegalCase> Cases = new List<LegalCase>();
}
LegalCase
class代表关系中的“多”
public class LegalCase
{
public LegalCase() : this("", CaseType.ArbGeneral){}
public LegalCase(string casefabula, CaseType casetype)
{
CaseFabula = casefabula;
CaseType = casetype;
}
public int Id { get; set; }
public string CaseFabula { get; set; }
public CaseType CaseType { get; set; }
//public ICaseParticipant Client { get; set; }
public int? CompanyId { get; set; }
public ParticipantCompany Company { get; set; }
public int? PersonId { get; set; }
public ParticipantPerson Person { get; set; }
}
现在是查询:
using(var db = new CaseContext())
{
var QClients = db.Companies
.Where(x => x.IsClient == true)
//Exception: The expression 'y.Cases' is
// invalid inside an 'Include' operation,
// since it does not represent a property
// access: 't => t.MyProperty'. etc
.Include(y => y.Cases)
.ToList();
}
我试图将 y
显式转换为 ParticipantCompany
因为这是执行提示所暗示的:
To target navigations declared on derived types, use casting ('t => ((Derived)t).MyProperty') or the 'as' operator ('t => (t as Derived).MyProperty')
但它生成相同的异常:
using(var db = new CaseContext())
{
var QClients = db.Companies
.Where(x => x.IsClient == true)
//Same exception
.Include(y => (y as ParticipantCompany).Cases)
.ToList();
}
根据异常建议将 Cases
从字段更改为 属性:
public List<LegalCase> Cases { get; set; } = new List<LegalCase>();
来自docs:
Each entity type in your model has a set of properties, which EF Core will read and write from the database. If you're using a relational database, entity properties map to table columns.
By convention, all public properties with a getter and a setter will be included in the model.