Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架,无法创建类型为';XX';。在此上下文中仅支持基元类型或枚举类型_C#_Asp.net Mvc_Entity Framework - Fatal编程技术网

C# 实体框架,无法创建类型为';XX';。在此上下文中仅支持基元类型或枚举类型

C# 实体框架,无法创建类型为';XX';。在此上下文中仅支持基元类型或枚举类型,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我有一个返回viewmodel的方法,定义如下: if (studentId != null) { var eduRec = (_context.EducationalRecords.Where(x => x.StudentId == studentId)).ToList(); var guarnator = (_context.NextOfKinGuarantors.Where(x => x.StudentId == studentId)).ToList();

我有一个返回viewmodel的方法,定义如下:

if (studentId != null)
{
    var eduRec = (_context.EducationalRecords.Where(x => x.StudentId == studentId)).ToList();
    var guarnator = (_context.NextOfKinGuarantors.Where(x => x.StudentId == studentId)).ToList();

    model = (from stud in _context.Students
             join lga in _context.LocalGovts
                 on stud.LocalGovtId equals lga.LocalGovtId
             join st in _context.States
                 on lga.StateId equals st.StateId
             join acada in _context.AcademicRecords
                 on stud.StudentId equals acada.StudentId
             join dept in _context.Departments
                 on acada.DepartmentId equals dept.DepartmentId
             join faculty in _context.Falculties
                 on dept.FalcultyId equals faculty.FalcultyId
             join prg in _context.Programmes
                 on acada.ProgrammeId equals prg.ProgrammeId
             join lvl in _context.Levels
                 on acada.LevelId equals lvl.LevelId
             where acada.IsCurrentRecord == true && stud.StudentId == studentId
             select new StudentProfileViewModel()
             {
                 ContactAddress = stud.ContactAddress,
                 Department = dept.Name,
                 Disability = stud.Disability,
                 Othernames = stud.Othernames,
                 FirstName = stud.FirstName,
                 Surname = stud.Surname,
                 Programme = prg.Name,
                 RegistrationNumber = stud.RegistrationNumber,
                 Dob = stud.Dob,
                 EducationalRecords = eduRec,
                 Email = stud.Email,
                 Faculty = faculty.Name,
                 Gender = stud.Gender,
                 HomeAddress = stud.HomeAddress,
                 Level = lvl.Name,
                 LocalGoverment = lga.Name,
                 MaritalStatus = stud.MaritalStatus,
                 Phone = stud.Phone,
                 Religion = stud.Religion,
                 StateName = st.Name,
                 NextOfKinGuarantors = guarnator
             }).FirstOrDefault();
}
运行应用程序时,我收到以下错误消息:

无法创建“Portal.Models.EducationalRecord”类型的常量值。在此上下文中仅支持基元类型或枚举类型


EducationalRecords
的定义是一个列表。

您的LINQ语句必须可翻译为有效的SQL查询,因此您必须小心调用。例如,如果您尝试调用用C#编写的某个随机方法,该方法可能无法转换为有效的SQL,因此您将得到一个错误

在您的例子中,它抱怨试图使用单独的LINQ语句填充
EducationalRecords
,这显然无法转换为单个SQL语句

从LINQ语句中删除此行:

EducationalRecords = eduRec,
填充
模型后,分别获取
教育记录

if (model != null)
   model.EducationalRecords =
      _context.EducationalRecords.Where(x => x.StudentId == studentId)).ToList();

Grant已经在您发布的代码中处理了这个问题,但是对于整个问题有一个更好的解决方案。将导航属性用于即时加载。这将使您不必进行三次单独的数据库往返以收集此信息


MSDN有关如何在code first、model first和db first中创建导航属性的信息。

我将创建
StudentProfileViewModel
,然后将项目添加到
EducationalRecords
属性(这是一个列表)。Simply EF无法将列表转换为有效表达式(“…仅支持基元类型或枚举类型…”)。的可能重复项