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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:04:50+00:00 2026-05-27T15:04:50+00:00

If I have use database first to build a context, and instantiate this context

  • 0

If I have use database first to build a context, and instantiate this context twice. I want to query a specific entity from the first context and add it to the second context, what’s the different between using AddObject and Attach.
eg.

Student stu = context1.Students.First();
context1.Detach(stu);
context2.Attach(stu);

and

Student stu = context1.Students.First();
context1.Detach(stu);
context2.Students.AddObject(stu);

What’s the difference between them?
Thanks in advance!

  • 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-27T15:04:51+00:00Added an answer on May 27, 2026 at 3:04 pm

    The Attach method will attach the object or object graph in Unchanged state. That means if you don’t do any modifications to the object after you attach it, EF will not issue any Update/Delete commands for that object when you call SaveChanges() method.

    But when you use AddObject method EF will insert the object as a new entity in SaveChanges() method.

    If the context2 is connected to a different database and you want to copy the instance then you can use AddObject. Otherwise use the Attach method.

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

Sidebar

Related Questions

I have an C# form application that use an access database. This application works
We have a database library in C# that we can use like this: DatabaseConnection
I have to build such application in which i have to use the database
Have a use case wherein need to maintain a connection open to a database
I have quite a problem concerning the use of relational database concepts in Delphi
Where I have more than one table in my database for use with (similar,
Anyone have a PL-SQL statement that i can use to generate database & tables
I have a problem in php code inserting values into database (I use PHPMyAdmin).
I have made a registration program. Making use of mysql database. Can I still
I have a database table which is full-text indexed and i use the CONTAINS-function

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.