thefrozencoder

Programming and Technology blog

Type 'ContosoUniversity.DAL.Department' could not be found

So I was going through the tutorial Handling Concurrency with the Entity Framework in an ASP.NET MVC Application when I encountered this error on step 7 of 10.

Type 'ContosoUniversity.DAL.Department' could not be found. Make sure that the required schemas are loaded and that the namespaces are imported correctly. Near type name, line 1, column 132.

The error is thrown on the following line:

var databaseValues = (Department)entry.GetDatabaseValues().ToObject();

This error is due to a bug in EF 4.1 where you separate the DAL and Model Entities out into different namespaces and is referenced here EF4.1 CodeFirst - entry.GetDatabaseValues() throw EntitySqlException.

To fix this issue I came up with this workaround:

Replace the lines:

var databaseValues = (Department)entry.GetDatabaseValues().ToObject(); 
var clientValues = (Department)entry.Entity;

with the following lines:

var clientValues = (Department)entry.CurrentValues.Clone().ToObject();
entry.Reload();
var databaseValues = (Department)entry.CurrentValues.ToObject();

It seems to work ok for simple concurrency checks and it allows you to continue on with the tutorial.