Doubt about which Inheritance to use in the Entity Framework

Asked

Viewed 70 times

2

I only had the opportunity to use the EF basically 1x. Therefore, I am with many doubts, even consulting a lot google. I have a client application that will take various data from the logged user’s Facebook...

Playing 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 of using EF code first to make my life easier. I then read about Heritages and found the most suitable for my use. However, I believe that my class would have to change to 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 right? Would that be the right one or with you with my informed class first? If yes, which would be the most correct to implement ? TPT ?

I also read about Complex Types.

But the question remains....

1 answer

1

It is more interesting to use inheritance, since the principle of reusability is one of the pillars of object orientation. I will make a superficial comment of your proposal using inheritance and you make the other necessary modifications, such as relocating the properties that Userdata owned before you made the inheritance.

Taking into account the fact that the Entity Framework understands inheritance, you put an id like int and another as string It’s redundancy even if they have different types. Normally the property Id is the name of the property that represents the primary key, so I recommend keeping only one in the superclass.

That point I will discuss and you make the expert decision. Theoretically, the Name property would be used in both entities, but if Userdata’s goal is to be the superclass of all entities in the system (not only these two), it is not interesting that it possesses this property since not all entities have Name. However, if Userdata’s goal is to be the superclass of only these two entities, it is interesting that the super class possesses this property.

More about: https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementing-inheritance-with-the-entity-framework-in-an-asp-net-mvc-application

  • In my case Userdata are the main user data...it will always have name... Thanks for the tip... Should I read about Complex Types and make use then ne?

  • When you say complex types this refers to: https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/complex-type ?

  • https://olavooneto.wordpress.com/2011/07/28/tipos-complexos-com-code-first/

  • I understood, it’s always interesting to read about gender issues, but I don’t recommend that you apply the concept of complex types in your example. The first view of the way you mapped it would not be interesting to have a single table for the three entities but two separate ones: Hometown and Location, since we would be breaking the third form of database normalization using a complex type in the example..

  • Now I noticed, you are confusing his example with inheritance, notice that in the example of complex type he does not use inheritance and everything becomes a single entity, in the case of inheritance is a little different.

  • becomes a single entity, but the classes are created the same way as in the client model... which confuses me in which to adopt...

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.