Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7703453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:27:45+00:00 2026-05-31T23:27:45+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:27:46+00:00Added an answer on May 31, 2026 at 11:27 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A few days ago, I wrote about issues with implementing a ListView in ASP.NET.
few days ago i asked about how to get all running processes in the
A few days ago I asked this question about jquery ajax function invoking action
I recently (a few days ago) installed .NET 3.5 SP1 and subsequently an aspnet_client
few days ago I asked here about implementing USB. Now, If I may, would
Since a few days ago I've started to feel interested in Unit Testing and
A few days ago, I posted this question and this question asking about how
A few days ago I has asked a question on SO about helping me
A few days ago, I watched the video tutorial which explains how to use
I came accross an issue a few days ago which I'd completely forgotten about,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.