Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntityFramework 4, DbSet and ObjectContext

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) ?

like image 330
geek Avatar asked Mar 22 '12 12:03

geek


People also ask

What is difference between DbContext and ObjectContext?

Definition. DBContext is a wrapper of ObjectContext that exposes the most commonly used features of ObjectContext. In contrast, Object Context is a class of the core Entity framework API that allows performing queries and tracking the updates made to a database using strongly typed entity classes.

What is meant by DbContext and DbSet?

A DbContext instance represents a combination of the Unit Of Work and Repository patterns such that it can be used to query from a database and group together changes that will then be written back to the store as a unit. Has code to configure each DbSet where TEntity is a model e.g. Blog, Post.

What is DbSet used for?

A DbSet represents the collection of all entities in the context, or that can be queried from the database, of a given type. DbSet objects are created from a DbContext using the DbContext. Set method.

What is DbSet and entity set?

In Entity Framework Core, the DbSet represents the set of entities. In a database, a group of similar entities is called an Entity Set. The DbSet enables the user to perform various operations like add, remove, update, etc. on the entity set.

What is ObjectContext C#?

The ObjectContext class is the primary class for interacting with data as objects that are instances of entity types that are defined in a conceptual model. An instance of the ObjectContext class encapsulates the following: A connection to the database, in the form of an EntityConnection object.


1 Answers

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.

like image 102
CodingGorilla Avatar answered Jan 03 '23 11:01

CodingGorilla