How to implement inheritance with an EF database - first

I have this simple class:

CREATE TABLE [dbo].[Movie] ( [MovieId] INT IDENTITY (1, 1) NOT NULL, [Title] NVARCHAR (200) NULL, [Genre] NVARCHAR (200) NULL, [Date] DATE NULL, [Price] DECIMAL (18, 2) NULL, CONSTRAINT [PK_dbo.Movies] PRIMARY KEY CLUSTERED ([MovieId] ASC) ); 

Now I want to implement SpecialMovie ..., which has one additional property called Special .

I work with a basic approach.

I tried to find some guides, but nothing was really helpful.

I have a SQL Server 2008 book and it says nothing about inheritance ... is not inheritance an important thing in SQL? It is strange that he has nothing to inherit from ....

+4
source share
1 answer

The implementation of inheritance in EF is exactly the same as the inheritance of a normal class, so in this case:

 public class Movie { public int MovieID {get; set;} //..... } public class SpecialMovie : Movie { public int SpecialMovieID {get; set;} //..... } public class MovieContext : DbContext { public DbSet<SpecialMovie> SpecialMovies { get; set; } public DbSet<Movie> Movies{ get; set; } } 

Note that in a relational database such as SQL Server, there is no corresponding inheritance structure. Thus, EF will implement inheritance in several ways. The table for the Hierarchy is perhaps the most productive, but it will denormalize the data so that the data for the SpecialMovie and Movie classes are in the same table, and EF adds a descriminator field to distinguish between the two classes, EF also offers tables for each type and tables for a specific type. There is a good description of all three here 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 .

-3
source

All Articles