tutsblogguy
tutsblogguy

Reputation: 49

Entity Framework, unable to create a constant value of type 'XX'. Only primitive types or enumeration types are supported in this context

I have a method that returns viewmodel as defined below:

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

When I run the application I got this error message:

Unable to create a constant value of type 'Portal.Models.EducationalRecord'. Only primitive types or enumeration types are supported in this context

The definition of EducationalRecords is a list.

Upvotes: 0

Views: 2320

Answers (2)

just.another.programmer
just.another.programmer

Reputation: 8805

Grant has already dealt with the problem in the code you posted, but there's a better solution to the entire problem. Use a navigation property with eager loading. This will save you from making three separate DB round trips to the DB to gather up this info.

Here's MSDN info on how to create the navigation property in code first, model first, and db first.

Upvotes: 0

Grant Winney
Grant Winney

Reputation: 66469

Your LINQ statement has to be translatable into a valid SQL query, so you have to be careful what you're calling. For instance, if you try to call some random method you wrote in C#, that probably won't translate to valid SQL, so you'll get an error.

In your case, it's complaining about trying to populate EducationalRecords using a separate LINQ statement, which apparently it cannot translate into a single SQL statement.

Remove this line from your LINQ statement:

EducationalRecords = eduRec,

Get the EducationalRecords separately, after you populate model:

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

Upvotes: 1

Related Questions