Posts

Showing posts with the label Entity Framework

Unit Testing with a Mock Entity Framework DbContext and Fake DbSets

I'm a big fan of the Repository pattern, and use it hand-in-hand with dependency injection and the Query Object pattern. Lately I've been developing with Entity Framework, and using dependency injection to pass the DbContext instances into my repository classes. But one of the issues I ran into while trying to unit test one of my repository classes is that I couldn't figure out how to mock the DbSets that my DbContext exposes. I could create a mock of the DbContext, but was having trouble setting it up to return fake collections of entities. One of the problems is that DbSet has no constructor (it's created via a factory method). After much research and scouring of the web, here are the steps I learned to accomplish this.   Step One: Create a Fake DbSet Class That Implements IDbSet The DbContext exposes entities in DbSets. DbSet, in turn, implements the IDbSet interface. So we can create a class for use with our unit tests that, unlike DbSet, can be instantiated on ...

Entity Framework and the "Undefined column mapping" Exception

I've recently run across an exception that occurs while trying to update Entity Framework data models from their respective databases. The exception message is "Unable to generate the model because of the following exception: 'An error occurred while executing the command definition. See the inner exception for details. Undefined column mapping." I searched online and couldn't find any mention of this exception. This surprised me, considering the frequency with which I've been encountering it. I thought that perhaps it had something to do with the DbContext I'm using with Entity Framework 4.1 to create persistence ignorant POCO (plain old CLR object) classes. I tried a few things to alleviate the issue, but to no avail. As of now, the only solution I have to this problem is merely a workaround: delete all of the entities from the model and re-add them. I hope to eventually find a good solution to this problem, but with limited time and no other leads ...