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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:11:22+00:00 2026-05-12T21:11:22+00:00

I’m very new to the Entity Framework and trying to understand how to best

  • 0

I’m very new to the Entity Framework and trying to understand how to best architect my data layer in way that makes the most sense from the prospective of a developer. I’m working on a proof of concept using the AventureWorks database. I built a generic repository that I use to query any table in the database. Following is a short, but compilable (I hope), snippet (the actual code has methods to update, delete, etc.):

    /// <summary>
    /// A generic repository for working with data in the database
    /// </summary>
    /// <typeparam name="T">A POCO that represents an Entity Framework entity</typeparam>
    public class DataRepository<T> : IDisposable, IRepository<T> where T : class
    {
        /// <summary>
        /// The context object for the database
        /// </summary>
        private ObjectContext _context;

        /// <summary>
        /// The IObjectSet that represents the current entity. Used to query, add, modify and delete objects of the specified entity.
        /// </summary>
        private IObjectSet<T> _objectSet;

        /// <summary>
        /// Initializes a new instance of the DataRepository class
        /// </summary>
        public DataRepository()
            : this(new AdventureWorksEntities())
        {
        }

        /// <summary>
        /// Initializes a new instance of the DataRepository class
        /// </summary>
        /// <param name="context">The Entity Framework ObjectContext</param>
        public DataRepository(ObjectContext context)
        {
            _context = context;
            _objectSet = _context.CreateObjectSet<T>();
        }

        /// <summary>
        /// Gets all records as an IQueryable
        /// </summary>
        /// <returns>An IQueryable object, containing the results of the query</returns>
        public IQueryable<T> GetQuery()
        {
            return from x in _objectSet select x;
        }

        /// <summary>
        /// Releases all resources used by the WarrantManagement.DataExtract.Dal.ReportDataBase
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Releases all resources used by the WarrantManagement.DataExtract.Dal.ReportDataBase
        /// </summary>
        /// <param name="disposing">A boolean value indicating whether or not to dispose managed resources</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_context != null)
                {
                    _context.Dispose();
                    _context = null;
                }
            }
        }
    }

In my ASP.NET demo app, I can use the repository, as follows, to get an Employee record (notice the reference to Contact):

        using (DataRepository repository = new DataRepository())
        {
            this.GridView1.DataSource = from x in repository.GetQuery()
                                        select new
                                        {
                                            x.MaritalStatus,
                                            x.Contact.FirstName,
                                            x.Contact.LastName,
                                            x.BirthDate
                                        };
            this.GridView1.DataBind();
        }

This could work out well, but my concern is that the View now has knowledge of the database schema, which I don’t like. For example, if I were to decide to move the FirstName and LastName columns into the Employee table, I would have to rewrite all lines of code where I referenced the columns as I did in the snippet above. My preference is to change the conceptual model by creating properties off of the Employee class and have Entity Framework automatically perform the JOIN, as necessary.

Firstly, are there any “lessons learned” from anybody that has designed a model like I have proposed? Anything negative that I should look out for? Secondly, are there any alternatives that I should be aware of?

  • 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-12T21:11:22+00:00Added an answer on May 12, 2026 at 9:11 pm

    Part of what you’re running up against is ASP.NET. Having the view aware of the repository at all is suspicious. In MVC, for example, binding repository public types to a view model would be done in the controller, and the view would only know about the view model.

    But even in straight ASP.NET I’d suggest creating dedicated view/presentation models and projecting onto them using an L2E query, similar to what you’re doing now with the anonymous type. Using a presentation model instead of an anonymous type will allow you to move the query out of the Page and remove knowledge of the Entity type from the page.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.