VSO
VSO

Reputation: 12656

Property In Parent Class - Changing Property Name in Child Possible?

I am modifying existing code. We have a base class from which about 10 classes inherit. The base class has public long Id { get; set; }. Now is a good time to mention that the children classes are all Entity Framework models, so the Id becomes an auto-generated primary key.

I now need to add tables that will have one-to-one relationships. I was thinking of ways to over-write "Id", but it seems there is nothing - neither abstract or virtual let you change the name.

Is my only option to remove the Id property altogether and implement it separately in each individual child class, assuming I want the option to rename it SomethingId ("Something" being whatever the PK of the one-to-one table is).

Upvotes: 5

Views: 2899

Answers (1)

Sam Axe
Sam Axe

Reputation: 33738

If you want to rename something in a child class you can...

public class Child : Parent {

    public ChildID { 
        get { return base.Id; }
        set { base.Id = value; }
    }

}

You may need to then override the Id property if you want it hidden, or to apply an attribute like [NotMapped].

Upvotes: 5

Related Questions