Entity Base Class

Discussion of open issues, suggestions and bugs regarding Entity Developer - ORM modeling and code generation tool
Post Reply
pintookhara
Posts: 2
Joined: Wed 01 Aug 2012 01:23

Entity Base Class

Post by pintookhara » Wed 01 Aug 2012 01:35

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!

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Entity Base Class

Post by Shalex » Thu 02 Aug 2012 14:59

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.

pintookhara
Posts: 2
Joined: Wed 01 Aug 2012 01:23

Re: Entity Base Class

Post by pintookhara » Sun 05 Aug 2012 19:03

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.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Entity Base Class

Post by Shalex » Tue 07 Aug 2012 15:21

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>

Post Reply