Showing posts with label entity framework. Show all posts
Showing posts with label entity framework. Show all posts

Thursday, March 10, 2011

The worst thing about Entity Framework

Generally, I like Entity Framework, especially the newest version (part of Visual Studio 2010). The absolute worst thing about it is the error message you can get when you are hooking-up your POCO files to an entity model. The error message is: "Mapping and metadata information could not be found for EntityType [...]"

EF tells you in which class the error is located, but that's it. It could be any of the properties that don't work! If you have a database table with 20 or 50 fields, good luck in tracking down the error. The best info I've found is that the error can be caused by:
  • Misspelled properties (case-sensitive!)
  • Properties missing in the POCO class
  • Type mismatches between the POCO and entity-type (e.g., int instead of long)
  • Enums in the POCO (EF doesn't support enums right now as I understand)
It has got to be the worst error message I've seen from Microsoft in a long time.

Friday, April 30, 2010

How to switch between SQL Server and SQL Server Compact with Entity Framework

I wanted to be able to save data to either a database on a SQL Server 2008 or an equivalent database on SQL Server Compact 3.5 with the minimum of hassle. Enter the new features of Entity Framework 4.0. I followed POCO in the Entity Framework: Part 1 - The Experience to turn off code generation.

I added two EDMs to my project, one for each database. I modified the ObjectContext class to accept different contexts, i.e.:

public class TestContext : ObjectContext
{
public TestContext(string contextName) : base("name=" + contextName, contextName)
{
_projects = CreateObjectSet<Project>();
}

public ObjectSet<Project> Projects
{
get
{
return _projects;
}
}
private ObjectSet<Project> _projects;
}


After that, I can now connect to either database by specifying either:


var compactContext = new TestContext("CompactEntities");

OR

var context = new TestContext("Entities");


Now I can use the same class files to both databases, and sync up the data in both with quite easily. The only redundancy is the EDMs, but since they are generated by Visual Studio, I don't care.

All too easy.