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

  • Home
  • SEARCH
  • 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 8614641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:10:06+00:00 2026-06-12T05:10:06+00:00

I have some Entity Framework objects which are also identified by their own ID.

  • 0

I have some Entity Framework objects which are also identified by their own ID.

I prefer to use reference comparison which is probably more reliable, especially for objects not on the DB yet.

Which is the gap between Int32 ID comparison and ByRef object comparison in terms of performance?

  • 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-12T05:10:07+00:00Added an answer on June 12, 2026 at 5:10 am

    The performance of comparing a reference and comparing an int will be about the same. The ID comparison requires an additional property access call on the stack. However, the performance difference between the two would be negligible.

    However, this should not be the determining factor as to how you determine equality. Equality should be determined based on what the object represents. If the object is defined by it’s ID property, then the ID property should be used to determine equality. If the object is defined by the composition of its values, then you should determine equality by comparing each of its component values.

    To better understand what I’m saying, consider the following class as an example:

     public class Lady { 
          public Lady(int id, string name, bool isMarried){
              this.ID = id;
              this.Name = name;
          }
          public int ID { get; private set; }
          public string Name { get; set; }
    
          public override int Equals(object other){  /* What goes here? */ }
          public override int GetHashCode(){  /* What goes here? */ }
     }
    

    Imagine that you’re app uses the Lady class like:

     void Main(){
          var JaneSmith = new Lady(id:12,name:"Jane Smith");
          var JaneSmithJones = new Lady(id:12,name:"Jane Smith-Jones");
     }
    

    In this case, the two objects might actually refer to the same person. Perhaps JaneSmith was created and cached before Jane got married and changed her name. But, now that her name has changed, and now both a value and reference comparison will fail. However, if we use ID equality, then this is okay, because we will know they are supposed to refer to the same person (and the same data source). We can then resolve which Lady instance is currently correct by abandoning both instances and reloading the Lady with ID == 12 from the database. By contrast, if we were using either reference equality or value equality, we would end up saving both objects, possibly overwriting the wrong data. Also, even if the data in both instances were identical and we only had two instances because we accidentally loaded JaneSmith from the database twice, the reference equality check would return false. It doesn’t seem quite right that, new Lady(12,"Jane").Equals(new Lady(12,"Jane")) should return false. It also makes caching virtually impossible, because you can never determine if a record has been cached or not.

    In general, using reference equality too compare persistent objects is a bad idea. However, ID equality is not always appropriate either. Neither is value equality. There are cases for both, and you have to decide what best represents the data you are manipulating. For more on this, this article provides a very basic concept of the difference between “Entities” and “Value Objects” (concepts from the Domain Driven Design methodology).

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

Sidebar

Related Questions

I am using Entity Framework 5 RC and i have some code that require
We have an entity framework model with has some inheritance in it. The following
I have got an Entity model which contains a collection of Message objects which
I am using Entity Framework POCO to generate some self-tracked data objects, and I
I have some objects which are accessed concurrently by RMI serialization. Recently I had
I have read some articles on POCO in the enttity framework but still don't
I have a WCF service that processes some messages. I use EntityFramework to retrieve
In my java application, I have some entity, some of them may have the
I want to persist my entity with ManyToMany relation. But i have some problem
Imagine you have an entity that has some relations with other entities and you

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.