简体   繁体   中英

How would you setup a one to many relationship of different types in Entity Framework code first?

This is my design. I'm thinking of where a parent object has a collection of child objects, where each child object can be a different type of object stored in an independent table in the database.

In my design, each day is aggregated into a Report object assigned to a User. This Report object can have multiple Activities. However, these Activities can be different types of objects. Like one activity may be food and another activity may be exercise. I'm curious if there's another way around this relationship than my current EF design.

My Report object cannot navigate to the children. But each child, like DbSet or DbSet has a link to the parent Report object. It's important to have the Report object because it links to goal information and helps to establish a base for aggregating data against goals.

Is there a way to setup a parent object so that it can have a child collection with multiple child objects, or setup the mappings so that if the parent Report is deleted then this will cascade to the child activities? Or would you go about this in a different way? Thanks

Inheritance is what you can use to accommodate different types into a single collection. Create a base Activity type, put the common properties there (along with a navigation property to it's parent class) and then create one subtype for each different activity type and add it's specific properties. I could not understand if the parent object and the report object are the same thing, so I will assume the aren't and define these two classes.

public class Parent
{
    public int Id { get; set; }
    public ICollection<Activity> Activities { get; set; }
}

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

public abstract class Activity
{
    public int Id { get; set; }
    public virtual Parent Parent { get; set; }
    public virtual Report Report { get; set; }
    // common activity properties
}

public class Food : Activity
{
    // Food specific properties
}

public class Excercise : Activity
{
    // Exercise specific properties
}

Then you can create a DbSet for the Parent , the Report and the Activity . There's no need to create dbsets for it's subtypes, but you can, if you wish so.

public DbSet<Parent> Parents { get; set; }
public DbSet<Report> Reports { get; set; }
public DbSet<Activity> Activities { get; set; }

You can query specific activity subtypes this way:

var indoorExercises = context.Activities.OfType<Exercise>().Where(e => e.IsIndoor);

You can get more information following this link: http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM