Entity Framework with MySQL learning Note one (inheritance)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.