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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:08:36+00:00 2026-05-17T21:08:36+00:00

In my application I have an entity which is being used essentially as a

  • 0

In my application I have an entity which is being used essentially as a complex many to many between my User and Project entities. I am trying to determine how to figure out if my service layer needs to add the entity to the context or attach the entity (for updating an existing entity) and I am at a loss of how.

This is easy to determine for most of my entities because of the Int Id field, if it’s zero the database hasn’t given it an identity value yet. This is not possible if it’s a composite primary key.

Does anyone have any suggestion on how to determine if an instance of an entity is new or an update to an existing record?

Edit: I forgot to mention, these entities are POCOs built for code-first, so I don’t have an EntityState property on the entity itself.

  • 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-17T21:08:36+00:00Added an answer on May 17, 2026 at 9:08 pm

    Yes, as the above answers state, you check the EntityState for the entity in the OSM.

    However, keep in mind this only works for entities attached to the context/graph.

    I’m currently working with detached entities (ASP.NET MVC), and because they are not attached to the graph, the EntityState is unchanged.

    In this case, i am doing a precautionary call to the DB to grab the entity by the key. If nothing is returned, i do an Add, otherwise i use ApplyCurrentValues to override the values, then do .SaveChanges

    I’m still wondering if this is the correct way, but thought i’d put it out there.

    I’m using POCO’s which have no change tracking, hence i have to do a bit more work.

    Since there is no EntityState for the POCO, you have to manually call into the OSM:

    var pocosInGraph = ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified)
    

    After you Attach/Add, your POCO should be in that collection.

    As i said though, if this is for a MVC application, your entities are detached on a HTTP POST, and thus the EntityState will still be unchanged.

    In my case, i manually set the EntityState after Attaching:

    ctx.Attach(poco);
    ctx.ObjectStateManager.ChangeObjectState(poco, EntityState.Modified);
    ctx.SaveChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET Dynamic Data application (using Entity Framework) in which I have
In our application, I have seen code written like this: User.java (User entity) public
I have a Core Data based application which is built around one main entity.
I have a large application that uses EJB 2.x entity beans (BMP). This is
I have in my web application an ADO.NET Entity-Framework *.edmx file. When I browse
I see an application have used Log.info = some info where are these logs
In my console application have an abstract Factory class Listener which contains code for
I have application which needs to use a dll (also written by me) which
I have an entity which has an ID field: @Id @Column(name = `U##ID_VOIE`) @GeneratedValue(generator
Overview: I'm designing a restaurant management application, and I have an entity called Order

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.