Joseph Caruana
Joseph Caruana

Reputation: 2271

Entity Framework table inheritance

I have the following 2 tables TableA and ArchivedTableA. Basically They are identical tables, but rows from TableA are moved to ArchivedTable to archive them.

How can I represent this in entity Framework so that these 2 classes inherit from the same entity. I am using POCOs.

Upvotes: 3

Views: 464

Answers (2)

ckal
ckal

Reputation: 3570

I would recommend not using inheritance in this situation. If you were to "archive" a record from TableA to ArchiveTableA it would still be in your base table.

Julie Lerman has a good MSDN article of some of the pitfalls of inheritance. http://msdn.microsoft.com/en-us/magazine/jj553510.aspx

Upvotes: 1

activebiz
activebiz

Reputation: 6230

I would create a base class with common properties for e.g.

     public class BaseEntity
    {
         public virtual int Id {get;set;}
         public DateTime CreatedOn  {get;set;}
    }

Then create POCO objects which inherit from this base class becouse they have common columns as follows ....

public class TableA : BaseEntity
{
   public string NewName {get;set;}
}

public class ArchivedTableA : BaseEntity
{
   public string Name {get;set;}
}

You obviously needs to do the DbSet Mapping etc in form of EF plumbing on top of this...

Upvotes: 0

Related Questions