Basically, there are 3 ways to represent an inheritance relationship in SQL.
respectively, 1 table Inheritance (TPH), 2 table Inheritance (TPC), 3 table Inheritance (TPT)
1 table:
Person
ID Type name Classroom office
1 student Keat 1 b NULL
2 teacher Xinyao Null LV2-T2
The advantage is not inner join fast, the disadvantage is a lot of null, waste space, column is very long not good-looking.
2 Table:
This is blind, don't learn. , probably there is no parent table, a lot of Word table, but each column is repeated write ... No words
3 table: (3 is only the code, in fact, the number of children to see how many tables, the child table ID is the same as the parent table)
Person
ID Name
Student
ID Classroom
Teacher
ID Office
So there is no null, just to inner join will be slow
Entity is implemented using the Fluent API.
3 Table mode
[Table (" Person")] Public classPerson {[Key] PublicInt32 ID {Get;Set; } Public stringName {Get;Set; } }//subclass do not write [table ("")] Public classStudent:person { Public stringClassroom {Get;Set; } } Public classTeacher:person { Public stringOffice {Get;Set; } }
Fluent API
protected Override void onmodelcreating (Dbmodelbuilder modelBuilder) { modelbuilder.entity<Person>(). On person Map<Student> (s = = s.totable ("Student")). Map additional 2 table, "Student" is tablename Map<Teacher> (t = t.totable ("Teacher ")); Base . Onmodelcreating (ModelBuilder); }
Insert words directly instantiate the word class can be
DB.STUDENTS.ADD (new Student { "keatkeat", " 1 B " }); Db. SaveChanges ();
View Code
1 Table mode:
[Table (" Person")] Public classPerson {[Key] PublicInt32 ID {Get;Set; } Public stringName {Get;Set; } } /*subclass Never Write [Table ()].*/ Public classStudent:person { Public stringClassroom {Get;Set; } } Public classTeacher:person { Public stringOffice {Get;Set; } }
Fluent API
protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder) {modelbuilder.entity<Person>() . Map<Student> (s = s.requires ("type"). HasValue ("Student")) . Map<Teacher> (M = M.requires ("type"). HasValue ("Teacher")); Base. Onmodelcreating (ModelBuilder); }
Entity Framework with MySQL learning Note one (inheritance)