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!
Entity Base Class
Re: Entity Base Class
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.
No changes in the template are needed.
-
- Posts: 2
- Joined: Wed 01 Aug 2012 01:23
Re: Entity Base Class
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.
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
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:pintookhara wrote:How to assign primary key value of customer class to Id property of base entity class?
Code: Select all
<id name="Id" type="Guid">
<column name="id" />
<generator class="assigned" />
</id>