Question

Is this possible?

Are there any examples? My searches have proved fruitless so far...

The reason I am using this approach is because I have to stick with a legacy database for my MVC 3 app but there are some quite complex associations and inheritance hierarchies so I want EF to generate the mappings instead of me doing it manually. I want to use the Repository pattern for unit testing. Any advice appreciated.

No correct solution

OTHER TIPS

Your search will become fruitful if you just search for "Repository pattern Entity Framework 4.1" or something and forget the term "Database First". It doesn't matter if you write your classes by hand (Code First) or generate them through the DbContext Generator (Database/Model First). In the end your repository will use those classes. There is no generator which would also write a meaningful repository for you - aside from the DbSets of your model which are specializations of a generic repositories. You must write it by hand based on the model classes and your business needs.

So, it doesn't affect the design of a repository whether you follow a Code First or Database/Model First approach.

About repository pattern with EF and unit testing I recommend to read this as a warning and starting point and follow the plenty of links in that answer:

Repository Pattern with Entity Framework 4.1 and Parent/Child Relationships

You could still use Code-first style if you want : code-first lets you reverse-engineer from an existing database to get you started. Or if you already have an EDMX, you can generate your code-first classes with a T4 template (packed with EF 4.1). Or, again, use T4 templates to generate POCOs and keep database-first as a strategy.

But I think you could still easily achieve a repository pattern even with database-first style. I think your main problem will be in the dependance to some entity framework DLLs if you do (which you could decide is not a problem).

I know this post is a day late and probably a dollar short. But I've had good luck so far implementing Repository Pattern on Database First by creating an instance of my DbContext in my controller and passing it into my Repository instance. Then I made sure my the methods in my Repositories return the Type of the my object when necessary.

I ran into two main issues while using Repository in this fashion. Firstly I had an issue with the default Dispose method on my controllers. Since I was calling the DbContext in my Repository, I had to inherit IDisposable and implement the Dispose method there. I used this tutorial as an example of that http://csharppulse.blogspot.in/2013/09/learning-mvc-part-6-generic-repository.html

The next issue I ran into was when it came time to update my objects. Because I had passed in the DbContext, my objects already existed. So I had to fashion my update method with logic that looked to to see if it already existed. I used this article to help with that An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top