在类中创建关联,而无需在数据库中创建额外的列

我有3个班级:大学,学院和学科 以及它们之间的关系:

public class University
    {
        public University()
        {
            Faculties = new List<Faculty>();    
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public string Location { get; set; } 
        [Association(ThisKey = "Id",OtherKey = nameof(Faculty.UniversityId),CanBeNull = true,Relationship = Relationship.OneToMany,IsBackReference = true)]
        public ICollection<Faculty> Faculties { get; set; }

    }

public class Faculty
    {
        public int Id { get; set; }
        public string Code { get; set; }
        public string FacultyName { get; set; }
        public string Direction { get; set; }
        public int Grant { get; set; }
        public int Contract { get; set; }
        public int? UniversityId { get; set; }

        [Association(ThisKey = "UniversityId",OtherKey = "Id",Relationship = Relationship.ManyToOne,BackReferenceName = "Faculties")]
        public University University { get; set; }

        [Association(ThisKey = "Id",OtherKey = "FacultyId",Relationship = Relationship.OneToOne,IsBackReference = true)]
        public Subject Subject { get; set; }

    }

public class Subject
    {

        [Column(Name = "Id")]
        public int SubjectId { get; set; }
        public string FirstSubject { get; set; }
        public string SecondSubject { get; set; }
        public string ThirdSubject { get; set; }

        public int? FacultyId { get; set; }
        [Association(ThisKey = "FacultyId",CanBeNull = false,BackReferenceName = "Subjects")]
        public Faculty Faculty { get; set; }
    }

我这样加载大学:

public IQueryable<University> Universities => context.University.LoadWith(x => x.Faculties);

我想在大学和学科之间建立联系以便称呼它:

public IQueryable<University> Universities => context.University.LoadWith(x => x.Faculties).LoadWith(m=>m.Subjects);

我尝试在大学课堂上这样做:

public static Expression<Func<University,IDataContext,IQueryable<Subject>>> Expression()
        {
            return (u,db) => db.GetTable<Subject>().
            Where(x => u.Faculties.
            Any(m => m.Id == x.FacultyId));
        }

        [Association(QueryExpressionmethod = nameof(Expression))]
        public ICollection<Subject> Subjects { get; set; }

但是我遇到一个例外:

  

'x.FacultyId'无法转换为SQL。

zn0282 回答:在类中创建关联,而无需在数据库中创建额外的列

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3148921.html

大家都在问