Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

c# - 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". Here's my context and model classes:

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.

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

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:

To target navigations declared on derived types, use casting ('t => ((Derived)t).MyProperty') or the 'as' operator ('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();
        }   
question from:https://stackoverflow.com/questions/65874906/the-expression-y-cases-is-invalid-inside-an-include-operation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change Cases from being field to property as the exception suggests:

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. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...