【问题标题】:The expression 'y.Cases' is invalid inside an 'Include' operation“包含”操作中的表达式“y.Cases”无效
【发布时间】:2021-04-28 16:13:48
【问题描述】:

我有一对多的关系数据库。 DbSet Companies 为“一”,DbSet Cases 为“多”。这是我的上下文和模型类:

数据库上下文

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 类继承自 Participant 类。

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 类代表关系中的“许多”

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,因为这似乎是执行提示所暗示的:

要定位在派生类型上声明的导航,请使用强制转换 ('t => ((Derived)t).MyProperty') 或 'as' 运算符 ('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();
        }   

【问题讨论】:

    标签: c# linq entity-framework-core-5


    【解决方案1】:

    按照异常提示将Cases 从字段更改为属性:

    public List<LegalCase> Cases { get; set; } = new List<LegalCase>();
    

    来自docs:

    模型中的每个实体类型都有一组属性,EF Core 将从数据库中读取和写入这些属性。如果您使用的是关系数据库,实体属性将映射到表列。

    按照惯例,所有带有 getter 和 setter 的公共属性都将包含在模型中。

    【讨论】:

      猜你喜欢
      • 2018-12-02
      • 2017-04-11
      • 2017-06-18
      • 2017-10-19
      • 2021-03-07
      • 2017-05-16
      相关资源
      最近更新 更多