Page 1 of 1

Entity Base Class

Posted: Wed 01 Aug 2012 01:35
by pintookhara
I am using Entity Developer with Nhibernate template.

I want to know how to set base class for my entities.

Basically I am trying to following:
public abstract class BaseEntity<TId>
{ }
public class Customer : BaseEntity<Guid> //pass primary key datatype to base class
{ }

Can you please suggest what changes I need to do in the template to accomplish this.

I will appreciate any help!

Re: Entity Base Class

Posted: Thu 02 Aug 2012 14:59
by Shalex
As we understood, your BaseEntity<TId> class is defined in some *.cs file or assembly and available in your project. In this case create the Customer class on your diagram, assign the BaseEntity<TId> value to class' Entity Base property. Define the Primary Key property for the Customer class (to make the model be valid) and set PK's Unimplemented attribute to True (to exclude the definition and initialization code of the corresponding property in the class).

No changes in the template are needed.

Re: Entity Base Class

Posted: Sun 05 Aug 2012 19:03
by pintookhara
Thanks Shalex for your response.

I was able to add base entity and set PK to unimplemented attribute as true.

How to assign primary key value of customer class to Id property of base entity class?

Here is what I trying to achieve:
public class Customer : BaseEntity<Guid>
{
// The CustomerID 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 CustomerID { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual int Age { get; set; }
}
public abstract class BaseEntity<TId>
{
public virtual TId Id { get; protected set; }
public override bool Equals(object obj)
{
return Equals(obj as Entity<TId>);
}
}
In the code snippet, CustomerID is not required any more, but how to set value of Id property in the base class.

Re: Entity Base Class

Posted: Tue 07 Aug 2012 15:21
by Shalex
pintookhara wrote:How to assign primary key value of customer class to Id property of base entity class?
The Id property of a base entity class will be inherited by Customer. To generate a correct mapping, make the following settings for your Customer.CustomerID property: Unimplemented=true and Name=Id. As a result, no PK property will be generated in the Customer class, but there will be a correct mapping in *.hbm.xml:

Code: Select all

    <id name="Id" type="Guid">
      <column name="id" />
      <generator class="assigned" />
    </id>