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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:11:58+00:00 2026-06-12T06:11:58+00:00

Example: I am entering a new invoice. For this invoice I need to enter

  • 0

Example:

I am entering a new invoice. For this invoice I need to enter a customer. Lets assume that we retrieved a list of customers:

var list = Context.Set<Customer>().ToList();

Here I see two issues:

1) I do not need to bring all information for customer, I only need Id, Code and Name

2) Customer in current DbContext is read-only, so it would be nice if it is possible to tell DbContext not to monitor their states, to improve performance.

Questions:

1) Can we load only partial data for customer, but still be able to assign it to Invoice (see code bellow)?

2) Can we tell DbContext not to monitor Customers for changes, and still be able to do this:

Invoice.Customer = CustomerList[10];
  • 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-12T06:11:59+00:00Added an answer on June 12, 2026 at 6:11 am

    There’s not a direct way to do exactly what you want, but you might be able to achieve your goals with some compromise.

    I do not need to bring all information for customer, I only need Id,
    Code and Name

    There isn’t a way for EF to create a partially loaded entity, but you could create an anonymous type:

    Context.Customers.Select(c => new {Id = c.CustomerId, Code = c.Code, Name = c.Name}).Tolist()
    

    If you could live with the new anonymous type then use that, or you could then iterate through that list, creating actual customer objects.

    Customer in current DbContext is read-only, so it would be nice if it
    is possible to tell DbContext not to monitor their states, to improve
    performance.

    EF provides an Extension of AsNoTracking() which will do exactly what you’re looking for:

    var list = Context.Set<Customer>().AsNoTracking().ToList();
    

    Depending on what you choose from above, the following code may change, but this code does achieve what you’re looking for. Partially loads the customer, but still allows you to attach the customer to the invoice.

    Note: You’ll need to attach the customer to your context before you can use it, and then setting it to a state of Unchanged will prevent it from overwriting exiting data.

            m = new Model();
    
            var list = m.Customers.Select(c => new {Id = c.CustomerId, Code = c.Code, Name = c.Name});
    
            List<Customer> customerList = new List<Customer>();
            foreach (var item in list)
            {
                customerList.Add(new Customer()
                    {
                        CustomerId = item.Id,
                        Code = item.Code,
                        Name = item.Name
                    });
            }
    
            Invoice i = new Invoice();
            var customer = customerList.First();
            m.Customers.Attach(customer);
            m.Entry(customer).State = EntityState.Unchanged;
    
            i.Customer = customer;
    
            m.Invoices.Add(i);
            m.SaveChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this program that returns a factorial of N. For example, when entering
I am entering a realm that is new to me, but basically I need
I am using this fantastic example of a jQuery editable invoice as a template
I'm new to this so I'm hoping that this is just a issue of
For example, the users of our app want to search products by entering a
OK. So, let's take PHP as an example. I'm entering a URL in the
Example: print max(chain_length(i) for i in xrange(1,10001)) This returns the maximum/biggest chain_length (an arbitrary
I have a form on a website that needs validating before entering the form
I am new to C# and I need to be able to access a
I was wondering how to format the source to allow this example : I

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.