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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:55:44+00:00 2026-06-07T03:55:44+00:00

I am fairly new to Entity Framework and investigating converting some legacy data access

  • 0

I am fairly new to Entity Framework and investigating converting some legacy data access code to using EF. I want to know if the following is possible in EF and if yes how.
Say I have a Customer table like this

CustomerId | ProductId | StartDate | EndDate
--------------------------------------------
100        | 999       | 01/01/2012| null

Say I also load Product data from somewhere else (like an XML file) as a cache of product objects.

public class Customer
{
    public int CustomerId {get;set;}
    public int Product {get;set}
    public DateTime StartDate {get;set;}
    public DateTime? EndDate {get;set;}    

}

public class Product
{
    public int ProductId {get;set;}
    public int Description {get;set}
}

Currently in CustomerDal class the method uses a StoredProc to get a Customer object like this

Customer GetCustomer(int customerId)
{
    // setup connection, command, parameters for SP, loop over datareader
    Customer customer = new Customer();
    customer.CustomerId = rdr.GetInt32(0);
    int productId = rdr.GetInt32(1);
    // ProductCache is a singleton object that has been initialised before
    customer.Product = ProductCache.Instance.GetProduct(productId);
    customer.StartDate = rdr.GetDateTime(2);
    customer.EndDate = rdr.IsDbNull(3) ? (DateTime?)null : rdr.GetDateTime(3);
    return customer;
}

My question is this possible using EF when it materializes the Customer object it sets the Product property not from the DB but by another method, in this case from an in memory cache. Similary when saving a new Customer object it only gets the ProductId from the Products property and saves the value in DB.

  • 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-07T03:55:46+00:00Added an answer on June 7, 2026 at 3:55 am

    If you attach your product instances to the EF context then when loading a Customer the Product property will be automatically filled from memory without a query to database as long as the product that is associated to the customer is already attached.

    For example, starting with these entities:

    public class Customer
    {
        public int Id { get; set; }
        public int ProductId { get; set; }
        public string Name { get; set; }
        public Product Product { get; set; }
    }
    
    public class Product
    {
        public int Id { get; set; }
        public string Description { get; set; }
    }
    

    Products will be available globally, for simplicity, lets make it a static class:

    public static class CachedProducts
    {
        public static Product[] All
        {
            get
            {
                return new Product[] { new Product { Id = 1, Description = "Foo" } };
            }
        }
    }
    

    With this in mind we just need to assure that every EF context starts with all the products attached to it:

    public class CustomerContext : DbContext
    {
        public CustomerContext()
        {
            // Attach products to context
            Array.ForEach(CachedProducts.All, p => this.Products.Attach(p));
        }
        public DbSet<Customer> Customers { get; set; }
        public DbSet<Product> Products { get; set; }
    }
    

    And finally, to make the sample complete and runnable we seed the database, request a customer and print the associated product description:

    public class DatabaseInitializer : CreateDatabaseIfNotExists<CustomerContext>
    {
        protected override void Seed(CustomerContext context)
        {
            var p = new Product { Id = 1, Description = "Foo" };
            var c = new Customer { Id = 1, Product = p, Name = "John Doe" };
    
            context.Customers.Add(c);
            context.SaveChanges();
        }
    }
    
    
    class Program
    {
        static void Main(string[] args)
        {
            Database.SetInitializer<CustomerContext>(new DatabaseInitializer());
    
            using (var context = new CustomerContext())
            {
                var customer = context.Customers.Single(c => c.Id == 1);
    
                Console.WriteLine(customer.Product.Description);
            }
        }
    }
    

    If you attach a profiler to SQL Server you will notice that the customer is loaded from database but no query is performed to obtain the product since it is already attached to the context. This works when loading a customer and also when saving a new customer with an associated product.

    Disclaimer: I’m not an EF expert so this approach may have some undesired side effects that I’m unable to consider.

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

Sidebar

Related Questions

I am fairly new to entity framework and I want to know what is
I'm fairly new to entity framework and patterns. How would I insert data into
I am fairly new to Entity framework as well as LINQ. I have used
I've been doing some research on using ASP.NET MVC and Entity Framework together for
Fairly new to asp.net Mvc and jquery. Have the following code working fine, but
I am using the Entity Framework 4.2 and I have a fairly serious performance
Let me explain the title. I'm using Entity Framework Code First in an application
I'm fairly new to C++ standard library and have been using standard library lists
Being new to the Entity Framework, I'm really rather stuck on how to proceed
First some brief background: I have an existing ASP.NET MVC 1 application using Entity

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.