Using entity infrastructure with custom subclasses

I am using C # entity structure to map data to sql server for a project. I am trying to understand how to use the entity infrastructure to automatically create custom subclasses based on the discriminator field, without requiring the subclass objects to have tables.

Example:

In the entity structure, I have an object A that has a type field. I also have the following

 public class B : A { } public class C : A { } 

I am looking for a way to make an entity structure a sign that the type field is, for example, B , and then instantiate the class B Classes do not have data that must be stored in the database. These are just wrappers for rows in another table.

Example:

I have a table of item objects. Each object of the object has a set of element parameters and an element type field.

There are different types of elements (text, radio, checkbox), and they are all displayed differently using the abstract render() method. Each of these subtypes also has a specific set of parameters required for this object.

If I could subclass entities without having to map them to the database, I could have a TextItem, which is a subclass of Item, implement my render() method and add properties that map to each of the values ​​in the item’s parameter table, Then, when im pulls out a new element from the database, depending on the type field, it will automatically create my specific class based on the discriminator.

I did such things using DBML. I would like to use the entity infrastructure as it is more powerful, but there can be no way to do what I am looking for.

+4
source share
1 answer

This is called mapping a table to a hierarchy, and this is possible with EDMX and code first . To really make it work, your subclasses must be displayed, because EF must be aware of their existence in order to correctly convert the discriminator value to the correct type. The hierarchy of the entire class is mapped to a single database table.

0
source

All Articles