Can somebody help me resolve this exception:
Test method KravmagaTests.Model.Entities.StudentTest.Create_Valid_Student threw exception: System
Change your test method this way:
// ...
context.Save();
int newStudentId = student.Id;
// because the Id generated by the DB is available after SaveChanges
bool exists = context.Accounts.Any(a => a.Id == newStudentId);
Assert.IsTrue(exists);
Contains
doesn't work here because it checks if a particular object instance is in the context.Accounts
set. Translation of this check into SQL is not supported, only for primitive types (like the exception says). Any
just translates the filter expression you specify into SQL and passes it to the database.
The Entity Framework is trying to translate the context.Accounts.Contains(student) into an SQL statement (e.g.: "WHERE ... IN (...)"). It cannot translate it into an SQL statement because it only knows how to handle primitive types (int, string...) hence the exception.
You are probably trying to have the EF generate an SQL statement such as:
SELECT * FROM Accounts WHERE Id IN (1, 2, 3, 4, 5)
You can write such a LINQ To Entities statement as follows:
var studentIds = new int[] { 1, 2, 3, 4, 5 };
var matches = from account in context.Accounts
where studentIds.Contains(account.Id)
select account;
For more information take a look at the following blog post:
http://blogs.msdn.com/b/alexj/archive/2009/03/26/tip-8-writing-where-in-style-queries-using-linq-to-entities.aspx
The blog post I mentioned offers a work around for the .NET 3.5 framework.