Entity Framework - Entity class field of use of inheritance

We often use the Category field in the data table to distinguish the type of record keeping, at objects in the scene, we generally will be that the evolution of inheritance relationship of a set of objects, to conform object-oriented design and use, then we Entity Framework environment, how to achieve such an entity to do with it? Here through some examples to illustrate the general usage scenarios.

Scene One: Data Sheet Peoples recorded a number of people, each person has a unique job categories, in this scenario, we need to follow the type of work to build a set of objects that people have been classified according to the working class.
First, create a data table as shown:

filling data are as follows:

[Suppose we were only four categories of people (actors, businessmen, programmers, athletes)]

Now we use VS2008 create PeopleModel.edmx model from the database, as shown:

VS2008 automatically we have created a solid People, but in this scenario we need to have a work object hierarchy based on class discrimination, so we add a new entity Actor (actor), businessman (businessman), Developer (programmer), Sporter (athletes ) they are inherited from the People (when creating an entity, specify the base type for the People), as shown:

Creating later, we get the following model:

now, we remove the map from the People JobCategory entity, because we need to use JobCategory People difference subclass, rather than as an attribute of the People. People entity set up as abstract (we assume that everyone has a job, huh).

Then, using the VS2008 "Mapping Details" view, and the condition setting table which maps for each subclass.

After each subclass similar modification is completed, our model're done .
1 <EntityType Name="Peoples">
2           <Key>
3             <PropertyRef Name="PeopleID" />
4           </Key>
5           <Property Name="PeopleID" Type="nvarchar" Nullable="false" MaxLength="50" />
6           <Property Name="PeopleName" Type="nvarchar" Nullable="false" MaxLength="50" />
7           <Property Name= "JobCategory"  the Type = "nvarchar"  Nullable = "to false"  the MaxLength = "50"  StoreGeneratedPattern = "Computed" /> . 8 </ the EntityType >
         


Here we can validate our model by codes.
ContractedBlock.gif ExpandedBlockStart.gif Code
class Program
    {
        
public static void Main(string[] args)
        {
            QueryDevelopers();
            Console.ReadLine();
        }

        
private static void QueryDevelopers()
        {
            
using (var context = new PeopleEntities())
            {
                var query 
= from c in context.Peoples
                            
where c is Developer
                            select c;
                
foreach(var c in query)
                {
                    Console.WriteLine(
"Actor Name : {0}", c.PeopleName);
                }

            }
        }
    }

class Program
    {
        
public static void Main(string[] args)
        {
            QueryDevelopers();
            Console.ReadLine();
        }

        
private static void QueryDevelopers()
        {
            
using (var context = new PeopleEntities())
            {
                var query 
= from c in context.Peoples
                            
where c is Developer
                            select c;
                
foreach(var c in query)
                {
                    Console.WriteLine(
"Actor Name : {0}", c.PeopleName);
                }

            }
        }
    }

转载于:https://www.cnblogs.com/crossingdawn/archive/2009/07/23/1529225.html

Guess you like

Origin blog.csdn.net/weixin_34090562/article/details/93409746