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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:17:01+00:00 2026-05-28T17:17:01+00:00

I have a property that contains a list from an entity in my model.

  • 0

I have a property that contains a list from an entity in my model. I want to add an item to this list but when I do, it’s added as detached. How can I add this object as attached?

using (var db = new fsEntities())
{
   var list = db.Products.Where(x => x.ID == 1).ToList();
   var p = new Product { Description = "New Item", Amount = 14};
   list.Add(p);   //the new item EntityState is detached
}

I know I can do this, db.AddToProducts(p), but in my scenario I want to add the object to the existing property with it’s EntityState as attached, then later perform a SaveChanges if necessary.

How can I do this?

  • 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-28T17:17:01+00:00Added an answer on May 28, 2026 at 5:17 pm

    You can attach the entity. This will be added to the context with Unchanged state.

    using (var db = new fsEntities())
    {
       var list = db.Products.Where(x => x.ID == 1).ToList();
       var p = new Product { Description = "New Item", Amount = 14};
    
       db.Attach(p);
    
       list.Add(p);   //the new item EntityState is detached
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model that contains a List<PhoneNumber> property. I use TryUpdateModel in my
say, I have a partial class that contains a custom property like this public
If I have a property that I want to let inheritors write to, but
I have a list of recipes obtained from a database that looks like this:
I have a model with a property that points to a file that contains
I have an object that contains a property: public Dictionary<string, List<Hotel>> CityHotels { get;
I have an NSColorWell , which is bound to a property that contains an
I have a Customer class that contains a property, MyProperty, which is of a
I have a CompositeControl that contains a DropDownList. I have set the AutoPostBack property
I have a Private property that I want to access in my MS Test

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.