S
S
stcmd042362016-11-17 17:36:46
OOP
stcmd04236, 2016-11-17 17:36:46

How to get data of inherited class in EF 6?

Good day to all!
How to get inherited objects in EF?
For example:

class Phone
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public double Price { get; set; }
    }

    class IPhone : Phone
    {
        public string Manufacturer { get; set; }
    }

Save to DB:
public void Create()
        {
            var phone = new Phone();
            var iphone = new Phone();

            Phones.Add(phone);
            Phones.Add(iphone);
        }

If you make a normal request i.e. without specifying a type, then ef returns all data in the form of Phone. How can I get all the data as it is. For example, if the data is stored in the form of an IPhone, then an IPhone, and if Samsung, then in the form of Samsung.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MrDywar Pichugin, 2016-11-17
@stcmd04236

You will study for a start mapping in the database of 3 types during inheritance.
TPH, TPT, TPC
https://msdn.microsoft.com/en-us/data/jj591617.aspx
metanit.com/sharp/entityframework Chapter 7. Inheritance in the Entity Framework
See how it is stored in the database.

S
saltydogd, 2016-11-17
@saltydogd

Check compatibility of .where types (p => p is ConcretePhone) and explode into appropriate collections

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question