简体   繁体   English

“包含”操作中的表达式“y.Cases”无效

[英]The expression 'y.Cases' is invalid inside an 'Include' operation

I have one-to-many relationship database.我有一对多的关系数据库。 DbSet Companies being "One" and DbSet Cases being "many". DbSet Companies是“一个”,而 DbSet Cases是“许多”。 Here's my context and model classes:这是我的上下文和 model 类:

Database Context数据库上下文

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 inherits from Participant Class. 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 represents "Many" in relationships 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; }
}

Now here's the Query:现在这里是查询:

        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();
        }

I tried to explicitly cast y to ParticipantCompany since it's what the prompt of the exection seems to suggest:我试图将y显式转换为ParticipantCompany ,因为这似乎是执行提示所暗示的:

To target navigations declared on derived types, use casting ('t => ((Derived)t).MyProperty') or the 'as' operator ('t => (t as Derived).MyProperty')要定位在派生类型上声明的导航,请使用强制转换 ('t => ((Derived)t).MyProperty') 或 'as' 运算符 ('t => (t as Derived).MyProperty')

But it generates the same exception:但它会产生相同的异常:

         using(var db = new CaseContext())
        {
            var QClients = db.Companies
                .Where(x => x.IsClient == true)
                 //Same exception
                .Include(y => (y as ParticipantCompany).Cases)
                .ToList();
        }   

Change Cases from being field to property as the exception suggests:例外情况表明,将Cases从字段更改为属性:

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

From the docs :文档

Each entity type in your model has a set of properties, which EF Core will read and write from the database. model 中的每个实体类型都有一组属性,EF Core 将从数据库中读取和写入这些属性。 If you're using a relational database, entity properties map to table columns.如果您使用的是关系数据库,则将实体属性 map 转换为表列。

By convention, all public properties with a getter and a setter will be included in the model.按照惯例,所有带有 getter 和 setter 的公共属性都将包含在 model 中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 该表达式在“包含”操作中无效,因为它不代表属性访问: - The expression is invalid inside an 'Include' operation, since it does not represent a property access: InvalidOperationException:表达式在“包含”操作中无效,因为它不代表属性 - InvalidOperationException: The expression is invalid inside an 'Include' operation, since it does not represent a property 表达式“x.Taken”在“包含”操作中无效,因为它不代表属性访问:“t =&gt; t.MyProperty” - The expression 'x.Taken' is invalid inside an 'Include' operation, since it does not represent a property access: 't => t.MyProperty' 在“包含”操作中无效,因为它不代表属性访问:“t =&gt; t.MyProperty” - Invalid inside an 'Include' operation, since it does not represent a property access: 't => t.MyProperty' EF Core 过滤包含:“包含中使用的 Lambda 表达式无效” - EF Core Filtered Include: “Lambda expression used inside Include is not valid” 在Include中使用的Lambda表达式无效 - Lambda expression used inside Include is not valid 如何在Lambda表达式中指定包含 - How to specify an Include inside a lambda expression 二进制表达式 Class 在比较运算符上抛出无效操作异常 - Binary Expression Class throws Invalid Operation Exception on Comparison Operators Include 中使用的 Lambda 表达式无效。 包括不工作 - Lambda expression used inside Include is not valid. Include not working 是否可以在 Linq 或 Lambda 表达式中包含 Where 或 Any 表达式? - Is it possible to have Where or Any expression inside Include in Linq or Lambda expression?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM