zoukankan      html  css  js  c++  java
  • Entity Framework with MySQL 学习笔记一(继承)

    基本上sql中要表示继承关系有3中方式.

    分别是,1表继承(TPH),2表继承(TPC),3表继承(TPT) 

    1表 : 

    Person 

    id  type  name  classroom  office 

    1  student  keat       1B      null

    2      teacher  xinyao    null       Lv2-T2

    好处是不用 inner join 快,坏处是null 很多,浪费空间, column很长不好看。 

    2表: 

    这个很瞎不要学 .. , 大概就是没有父表,字表很多,但是每个column都重复写...无言

    3表: (3只是代号,其实是看子类多少就多少表,子表的 id 是跟父表一样的)

    Person 

    id  name

    Student

    id  classroom 

    Teacher

    id  office 

    这样就没有null了,只是要inner join 会慢

    entity 是用 Fluent API 来实现的

    3表方式 

        [Table("person")]
        public class Person
        {
            [Key]
            public Int32 id { get; set; }
            public string name { get; set; }
        }
        //子类不要写 [table("")]public class Student : Person
        {
            public string classroom { get; set; }
        }public class Teacher : Person
        {
            public string office { get; set; }
        }

    Fluent API

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Person>().  //对Person
                Map<Student>(s => s.ToTable("student")). //map 另外2个table , "student" 是tableName
                Map<Teacher>(t => t.ToTable("teacher"));
            base.OnModelCreating(modelBuilder);
        }

     insert 的话直接实例化字类就可以了 

        db.students.Add(new Student
        {
            name = "keatkeat",
            classroom = "1B"
        });
        db.SaveChanges();
    View Code

    1表方式 : 

        [Table("person")]
        public class Person
        {
            [Key]
            public Int32 id { get; set; }
            public string name { get; set; } 
         //publick string type {get ;set;} //这里是不可以这样写的,type是entity 和 sql 秘密沟通用的
    } /* 子类千万不要写 [Table()] 了 */ public class Student : Person { public string classroom { get; set; } } public class Teacher : Person { public string office { get; set; } }

    Fluent API

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Person>()
                    .Map<Student>(s => s.Requires("type").HasValue("student"))
                    .Map<Teacher>(m => m.Requires("type").HasValue("teacher"));
            base.OnModelCreating(modelBuilder);
        }

     属性 type 是 sql 和 EF 秘密沟通的值,我们是不能获取的。

     

    在获取数据或者做过滤的时候 

    var commentss = db.users.OfType<Salesman>().Where(s => s.salesmanSpecialColumn == "abc").ToList();
    var comments = db.comments.Where(c => c.user is Salesman && (c.user as Salesman).salesmanSpecialColumn == "abc").ToList();

    主要是用了 OfType , is , as 来强转

    另外 WebAPI 2 , OData v4 的 request url 是这样写的 : 

    /api/comments?$filter=user/EFDB.Salesman/salesmanSpecialColumn eq 'abc'

    for expand : /api/Singles?$expand=abstracts($expand=EFDB.AA/childs)

    另外, TPT 虽然比较 OO 不过也常出现Bug,尤其是你的继承很深很宽.

    TPT还有个坏处就是 join 表会很慢 。

    TPT 在做 checkconcurrent 时要注意,最好在基表加一个rowVersion column , 在 saveChanges 时set 这个column to isModified , 不然并出来的语句会有问题的。 

    所以通常我们还是会使用 TPH 来做项目 

    TPH 也是有问题的,虽然select不用Join,

    nullable 引起的问题 :浪费空间,sql table 不好看 , 对索引不利 , 不能强制验证不能为null. 

    (For MSSQL)

    此外nullable也不方便set UNIQUE (虽然可以用 filter fix)

    还有一种是类的继承 (完全没有写  data annotations vs fluent api)

    它表示在所有表都有这个基类的属性

    通常我们可以用来做 rowVersion , lastModified, createDatetime 这类基本的东西。

    需要注意的是如果有 creator_id (比如记入administrator_id) 遇到有关联的表有些情况要使用  [InverseProperty("administrator")] 来指定哦 ! 我之前就是被这个坑了一下。

    还有这种继承不能放在抽象类上(我没有研究为什么,就有bug). 所以比如有rowVersion 那些,在多层继承的类的最上层基类,直接copy paste 属性进去而不要使用继承. 

    更新 : 2015-07-11 

    抽象累被 OData json 化返回到前台,每个资源都会附带一个 "'@odata.type' : '#EFDB.Member'" 来表明它是属于那个子类。

    当我们在做 POST,PUT 的时候也是同样的道理,我们要告诉OData 明确的子类类型,不然OData是没有办法判断出来的。所以 POST 的json 也必须含有 "@odata.type" !

    更新 : 2015-09-26 

    类型一但创建了就不能够换了,EF并不支持,比如你想把 Teacher 换成 Student =.=” 

  • 相关阅读:
    AX 2012 Security Framework
    The new concept 'Model' in AX 2012
    How to debug the SSRS report in AX 2012
    Using The 'Report Data Provider' As The Data Source For AX 2012 SSRS Report
    Deploy SSRS Report In AX 2012
    AX 2012 SSRS Report Data Source Type
    《Taurus Database: How to be Fast, Available, and Frugal in the Cloud》阅读笔记
    图分析理论 大纲小结
    一文快速了解Posix IO 缓冲
    #转载备忘# Linux程序调试工具
  • 原文地址:https://www.cnblogs.com/keatkeat/p/4072644.html
Copyright © 2011-2022 走看看