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.

Comments (3) -

  • Iwan Effendi

    9/20/2011 5:56:21 AM |

    thank you!

  • Magento Designer

    9/24/2011 7:33:26 AM |

    This is an excellent post. I really appreciate with you. I enjoyed the post. Thanks for sharing.

  • jrummell

    10/6/2011 9:34:57 AM |

    Thanks for sharing this workaround. Hopefully they'll fix this for EF 4.2.

Comments are closed