activerecord with two similar tables

87 Views Asked by At

I have two Tables on my Database. They look like this (it's just a simple example, there are probably mistakes in the code)

create table Person
(
   id int,
   name nvarchar(50),
   surname nvarchar(50),
   street nvarchar(50),
   city nvarchar(50)
)

create table Organisation
(
   id int,
   name nvarchar(50),
   contact int Foreign Key Person(Id)
   street nvarchar(50),
   city nvarchar(50)
)

Now I want to create my ActiveRecord classes. But about 90% of the columns have the same information. Is there a way to create an abstract ActiveRecord class / Interface where I save my duplicated information? Something like:

[ActiveRecord]
public abstract class / interface Customer
{
    [PrimaryKey(Generator = PrimaryKeyType.Identity)]
    long Id { get; set; }

    [Property]
    string name { get; set; }

    [Property]
    string street { get; set; }

    [Property]
    string city { get; set; }
}

[ActiveRecord (Table = "Person")]
public class Person : Customer
{   
    [Property]
    public string surname { get; set; }
}

[ActiveRecord (Table = "Organisation")]
public class Organisation : Customer
{   
    [BelongsTo(Column = "contact", ForeignKey = "FK_Person_Contact")]
    public Person Contact { get; set; }
}
1

There are 1 best solutions below

0
wydy On