У меня есть три класса: университет, факультет и предмет и отношения между ними:
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", CanBeNull = true, Relationship = Relationship.ManyToOne, BackReferenceName = "Faculties")]
public University University { get; set; }
[Association(ThisKey = "Id", OtherKey = "FacultyId", CanBeNull = true, 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", OtherKey = "Id", CanBeNull = false, Relationship = Relationship.ManyToOne, 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.