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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:07:02+00:00 2026-06-09T02:07:02+00:00

I am trying to implement abstract base entity class which has overriden equals and

  • 0

I am trying to implement abstract base entity class which has overriden equals and GetHashcode…Here is my entity base class

public abstract class Entity<TId>
{

public virtual TId Id { get; protected set; }
protected virtual int Version { get; set; }

public override bool Equals(object obj)
{
  return Equals(obj as Entity<TId>);
}

private static bool IsTransient(Entity<TId> obj)
{
  return obj != null &&
         Equals(obj.Id, default(TId));
}

private Type GetUnproxiedType()
{
  return GetType();
}

public virtual bool Equals(Entity<TId> other)
{
  if (other == null)
    return false;

  if (ReferenceEquals(this, other))
    return true;

  if (!IsTransient(this) &&
      !IsTransient(other) &&
      Equals(Id, other.Id))
  {
    var otherType = other.GetUnproxiedType();
    var thisType = GetUnproxiedType();
    return thisType.IsAssignableFrom(otherType) ||
           otherType.IsAssignableFrom(thisType);
  }

  return false;
}

public override int GetHashCode()
{
  if (Equals(Id, default(TId)))
    return base.GetHashCode();
  return Id.GetHashCode();
}

}

How does the value of entity base Id gets assigned?

The primary keys of my classes have different datatypes and also names are different for each class.
Here is the sample of my classes:

public class Product : Entity
{
    public virtual Guid ProductId { get; set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual Decimal UnitPrice { get; set; }
}

public class Customer : Entity
{
    public virtual int CustomerID { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual int Age { get; set; }
}

I am bit confused on how to set ID property of the base class. Can anyone please suggest me on this, I will appreciate any help.

  • 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-09T02:07:03+00:00Added an answer on June 9, 2026 at 2:07 am

    You just need to pass a type to the inherited base class.

    See comments in the entities:

    public class Product : Entity<Guid>
    {
        // The ProductId property is no longer needed as the
        // Id property on the base class will be of type Guid
        // and can serve as the Id
        //public virtual Guid ProductId { get; set; }
        public virtual string Name { get; set; }
        public virtual string Description { get; set; }
        public virtual Decimal UnitPrice { get; set; }
    }
    
    public class Customer : Entity<int>
    {
        // The CustomerID property is no longer needed as the
        // Id property on the base class will be of type int
        // and can serve as the Id
        // public virtual int CustomerID { get; set; }
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
        public virtual int Age { get; set; }
    }
    

    Now in your NHibernate mapping files, just specify what the database column is for your Id properties.

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

Sidebar

Related Questions

I'm trying to implement an identity map using generics. I have an abstract class,
I'm trying to do the following: public abstract BaseClass { public virtual void ReceiveEvent(Event
If in an abstract base class, there is a public/private method m1 and an
I'm trying to be lazy and implement the cast operators in the abstract base
I have an abstract base class which acts as an interface. I have two
I have a base class that has an abstract getType() method. I want subclasses
I'm trying to setup a base Repository class that can use the Entity Framework
I'm trying to use C++ abstract base class in the way similar with Java
I'm trying to create base class and force all subclasses to implement it's interface.
I'm trying implement A* Start path finding in my games(which are written with JavaScript,

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.