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 9198891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:23:46+00:00 2026-06-17T22:23:46+00:00

I am using Entity Framework 5 in VS.NET 2012 using a Database First approach.

  • 0

I am using Entity Framework 5 in VS.NET 2012 using a Database First approach. I’m also trying to implement a standard generic repository interface along with a Unit of Work to make sure all updates happen on a single context. Creating the repository, UoW, injecting it using DI are actually the pieces I know how to complete.

The problem I have is understanding how it correlates to the MyModel.Context.cs class below that was auto-generated from the Entity Framework 5 T4 POCO template:

public partial class AdventureWorks2008R2Entities : DbContext
{
    public AdventureWorks2008R2Entities() : base("name=AdventureWorks2008R2Entities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public DbSet<Person> People { get; set; }
    public DbSet<PersonPhone> PersonPhones { get; set; }
    public DbSet<PhoneNumberType> PhoneNumberTypes { get; set; }
}

What I think that is to occur, is that an instance of this class is what is to be sent to the constructor of my UoW class like below:

public UnitOfWork(IContext context)
{
    _context = context;
}

So my main question is, how the auto generated context class and my Repository I created (that uses UoW) are supposed to work together. I found some examples using code first in EF4, but nothing for EF5 using Database First approach with POCO generation and Repository pattern. If anyone knows of a good example on this please share.

Any help with clearing this up is appreciated, thanks!

  • 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-06-17T22:23:47+00:00Added an answer on June 17, 2026 at 10:23 pm

    Ok I figured one method out to solve this issue. To begin this method could actually work for Database 1st or Code 1st approaches as the Context is not modified and only an instance of it used.

    Some samples have the IUnitOfWork interface being applied directly to the auto generated AdventureWorks2008R2Entities class. However you would not want to do this because modifying auto-generated code that could be overwritten is not a great idea.

    The idea is to have the generic repository (or if not using a generic repository each individual repository) take in an instance of the EF Context like below:

    public GenericRepository(DbContext context)
    {
        this.context = context;
        this.dbSet = context.Set<TEntity>();
    }
    

    Now the IUnitOfWork Interface should declare the methods for EF and an instance of each repository type like below:

    public interface IUnitOfWork : IDisposable
    {
        GenericRepository<Person> PersonRepository { get; }
        GenericRepository<PersonPhone> PersonPhoneRepository { get; }
        void Save();
    }
    

    The implementation will new up an instance of the repository if it is not already instantiated like below:

    public class UnitOfWork : IUnitOfWork
    {
    private DbContext _context;
    private GenericRepository _personRepository;
    private GenericRepository _personPhoneRepository;

    public UnitOfWork(DbContext context)
    {
        _context = context;
    }
    
    public GenericRepository<Person> PersonRepository
    {
        get
        {
    
            if (this._personRepository == null)
            {
                this._personRepository = new GenericRepository<Person>(_context);
            }
            return _personRepository;
        }
    }
    
    public GenericRepository<PersonPhone> PersonPhoneRepository
    {
        get
        {
    
            if (this._personPhoneRepository == null)
            {
                this._personPhoneRepository = new GenericRepository<PersonPhone>(_context);
            }
            return _personPhoneRepository;
        }
    }
    
    public void Save()
    {
        try
        {
            _context.SaveChanges();
        }
        catch (DbEntityValidationException dbEx)
        {
            foreach (var validationErrors in dbEx.EntityValidationErrors)
            {
                foreach (var validationError in validationErrors.ValidationErrors)
                {
                    Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                }
            }
        }
    
    }
    
    #region Implementation of IDisposable
    
    /// <summary>
    /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
    /// </summary>
    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
    
    /// <summary>
    /// Disposes off the managed and unmanaged resources used.
    /// </summary>
    /// <param name="disposing"></param>
    private void Dispose(bool disposing)
    {
        if (!disposing)
            return;
    
        if (_disposed)
            return;
    
        _disposed = true;
    }
    
    private bool _disposed;
    #endregion
    

    }

    Lastly, to use the Unit of Work instance for your repositories, you reach through it to the appropriate repository instance and the UoW will take care of making sure all changes saved happen on the same context:

    _unitOfWork.PersonRepository.Insert(person);
    _unitOfWork.Save();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are using .Net Entity Framework to do our database related work. Our database
I'm using the Entity Framework (.NET 4.0) with SQLite as the underlying database and
I am using Entity Framework 4.1 code first and ASP.NET MVC 3 and I
I'm trying to accomplish the following using Entity Framework 4.1 in .Net 4; var
I'm using Entity Framework 4 Code First in my .net MVC 2.0 project and
Premise: I'm using Entity Framework, Repository Pattern and MVP for an ASP.NET project. Question:
Using Entity Framework 5.0.0 RC/EF 5.x DbContext Generator for C#/Visual Studio 2012 RC/.NET 4.0,
I'm using Entity Framework 5 on .NET 4.5 with SQL Server 2012. I have
I'm thinking about using Entity Framework in an ASP.NET application, using an Oracle database.
Am using Entity framework to connect database with my asp.net application. Here I've a

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.