Table per hierarchy – type 1

public class Course
{
[Key]
public int CourseId { get; set; }
public string Name { get; set; }
}
public class OnlineCourse : Course
{
public string URL { get; set; }
}
public class OfflineCourse : Course
{
public string Address { get; set; }
}

public class TPHContext:DbContext
{
public DbSet<Course> Courses { get; set; }
public TPHContext() : base(“TPH”)
{
Database.SetInitializer<TPHContext>(new DropCreateDatabaseIfModelChanges<TPHContext>());
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Course>()
.Map<OnlineCourse>(m => m.Requires(“Type”).HasValue(“OnlineCourse”))
.Map<OfflineCourse>(m => m.Requires(“Type”).HasValue(“OfflineCourse”));
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s