Doubt about which Inheritance to use in the Entity Framework

2

I just had the opportunity to use EF basically 1x. So, I have a lot of doubts, even though I'm consulting quite a bit with google. I have a client application that will pick up various Facebook data from the logged in user ...

Playing in Json2Charp I have the following classes:

    public class UserData
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        [JsonProperty("id")]
        public string idface { get; set; }
        public string name { get; set; }
        public string birthday { get; set; }
        public string email { get; set; }
        public Hometown hometown { get; set; }
        public Location location { get; set; }
        public Events events { get; set; }
        public Likes likes { get; set; }
        public Age_Range age_range { get; set; }
        public string gender { get; set; }
        public Picture picture { get; set; }
    }

    public class Hometown
    {
        public string id { get; set; }
        public string name { get; set; }
    }

    public class Location
    {
        public string id { get; set; }
        public string name { get; set; }
    }

    public class Events
    {
        public Datum[] data { get; set; }
        public Paging paging { get; set; }
    }

    public class Paging
    {
        public Cursors cursors { get; set; }
        public string next { get; set; }
    }

    public class Cursors
    {
        public string before { get; set; }
        public string after { get; set; }
    }

    public class Datum
    {
        public string description { get; set; }
        public string name { get; set; }
        public DateTime start_time { get; set; }
        public Place place { get; set; }
        public int attending_count { get; set; }
        public string id { get; set; }
        public string type { get; set; }
        public string rsvp_status { get; set; }
        public DateTime end_time { get; set; }
    }

    public class Place
    {
        public string name { get; set; }
        public LocationEvent location { get; set; }
        public string id { get; set; }
    }

    public class LocationEvent
    {
        public string city { get; set; }
        public string country { get; set; }
        public float latitude { get; set; }
        public float longitude { get; set; }
        public string state { get; set; }
        public string street { get; set; }
        public string zip { get; set; }
    }

    public class Likes
    {
        public Datum1[] data { get; set; }
        public Paging paging { get; set; }
    }

    public class Datum1
    {
        public string category { get; set; }
        public string name { get; set; }
        public int fan_count { get; set; }
        public string website { get; set; }
        public string id { get; set; }
        public LocationEvent location { get; set; }
        public string[] emails { get; set; }
    }


    public class Age_Range
    {
        public int min { get; set; }
    }

    public class Picture
    {
        public Data data { get; set; }
    }

    public class Data
    {
        public bool is_silhouette { get; set; }
        public string url { get; set; }
    }

When creating a webapi to connect with my Azure SQL, I thought about using EF code first to make my life easier. I read about Inheritance and found it most suitable for my use. However, I believe my class would have to change for example:

    public class UserData
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        [JsonProperty("id")]
        public string idface { get; set; }
        public string name { get; set; }
        public string birthday { get; set; }
        public string email { get; set; }

    }

    public class Hometown : UserData
    {
        public string id { get; set; }
        public string name { get; set; }
    }

    public class Location : UserData
    {
        public string id { get; set; }
        public string name { get; set; }
    }

    E assim por diante....

Am I correct? Is this the right thing to do or can I get my class informed first? If yes, what would be the most correct to implement? TPT?

I also read about Complex Types.

But the question remains ... what to use.

    
asked by anonymous 08.11.2017 / 01:11

1 answer

1

It is more interesting to use inheritance, since the principle of reusability is one of the pillars of object orientation. I'll make a cursory review of your proposal using inheritance, and you'll make the other necessary modifications, such as reallocating the properties that UserData had before you inherited.

Considering the fact that the Entity Framework understands inheritance, you put an id as int and another as string is a redundancy even if they have different types. Usually the Id property is the name of the property that represents the primary key, so I recommend keeping only one in the superclass.

This point I will discuss and you make the expert decision. Theoretically the Name property would be used in both entities, but if the purpose of UserData is to be the superclass of all entities of the system (not just these two), it is not interesting that it has this property since not all entities have Name. However, if the purpose of UserData is to be the superclass of only these two entities, it is interesting that superclass has this property.

More about: link

    
08.11.2017 / 02:27