EntityFramework 4, DbSet and ObjectContext
Asked Answered
P

2

6

few days ago i read tutorial about GenericRepository and Unit Of Work patterns http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application. I use web forms and i have EntityFramework CTP4 package installed. (I can't using EF 5).

I want to code generic repository for my project but i was stuck at this line:

this.dbSet = context.Set<TEntity>();

I know that this line doesn't work because a use ObjectContext in my project and database first approach. How can i deal with it? Can I code generic repository without migration to code first (which is not an option in my case) ?

Poinciana answered 22/3, 2012 at 12:51 Comment(0)
C
7

This is the equivalent for ObjectContext:

this.dbSet = context.CreateObjectSet<TEntity>();

Now this creates an ObjectSet<TEntity> rather than a DbSet<TEntity>, but for your pattern you can use it in the same way.

UPDATE

The ObjectSet class does not have a utility method like that matches the Find() method of the DbSet. In order to "Get by key" you would need to construct an EntityKey and use the ObjectContext.GetEntityByKey(), unfortunately that's not a really simple thing to do.

There really isn't a simple way to tackle this, that I've found. In my case what I've done is to base all of my entities from a common class (using custom T4 templates to generate the classes from the model). And then I can add a generic constraint to my repositories, like:

public class MyRepository<TEntity> where TEntity : MyEntityBaseClass

And then my common base class has an Id field which is inherited by all the entities so I can can simply do:

return myObjectSet.SingleOrDefault(x => x.Id == myId);

I'm sure there are other approaches, that might be a good topic for another question.

Carbonic answered 22/3, 2012 at 12:54 Comment(3)
Thanks for help. Now this creates an ObjectSet<TEntity> rather than a DbSet<TEntity>, but for your pattern you can use it in the same way. I have problem with find method in this line: dbSet.Find(id); How can i code this using ObjectSet ?Poinciana
so if Find by Id in ObjectSet is so hard why you didn't use DbContext ? I wonder which of this method should i use, try to create find method as you or maybe i should use DbContext ? I haven't written to much code so far in this project, so maybe it's better idea to use DbContext ? Could you give your opinion about it ? ThanksPoinciana
At the time that I started my application, EF Code First hadn't been released yet. Based on Ropstah's answer it looks like there are templates you can use to generate Code First style classes for Model First or Database First applications. I've never tried that approach, so I really cannot speak to whether or how well it works.Carbonic
T
4

1. You want to add the DbContextGenerator template to your visual studio templates:

DbContextGenerator template

2. After this make sure you clear out the default generation tool on your .edmx file.

Code generation tool

3. Now you can implement the GenericRepository pattern as you wish.

Trismus answered 22/3, 2012 at 12:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.