zoukankan      html  css  js  c++  java
  • Lerning Entity Framework 6 ------ Introduction to TPH


    Sometimes, you have created two models. They have the same parent class like this:

    public class Person
    {
        public int PersonId { get; set; }
    
        public string PersonName { get; set; }
    }
    
    public class InsidePerson : Person
    {
        public string Title { get; set; }
    
        public string Department { get; set; }
    }
    
    public class OutsidePerson : Person
    {
        public string CompanyName { get; set; }
    }
    

    After you execute the command Update-Database in nuget command line, Entity Framework will create one table named people:

    图片.png-6.6kB

    The Discriminator column is created for discriminating what model dose current row represent. this model creating type is called TPH(Table per Hierarchy Inheritance). Let's do a test. Add some codes in main function:

    static void Main(string[] args)
    {
        using (MyDbContext db = new MyDbContext())
        {
            Person insidePerson1 = new InsidePerson()
            {
                PersonName = "InsidePerson1",
                Title = "Manager",
                Department = "development"
            };
            db.People.Add(insidePerson1);
    
            InsidePerson insidePerson2 = new InsidePerson()
            {
                PersonName = "InsidePerson2",
                Title = "Manager",
                Department = "development"
            };
            db.People.Add(insidePerson2);
    
            InsidePerson insidePerson3 = new InsidePerson()
            {
                PersonName = "InsidePerson3",
                Title = "Manager",
                Department = "development"
            };
            db.InsidePeople.Add(insidePerson3);
    
            Person outsidePerson1 = new OutsidePerson()
            {
                PersonName = "outsidePerson1",
                CompanyName = "Tencent"
            };
            db.People.Add(outsidePerson1);
    
            db.SaveChanges();
        }
    }
    

    Let's look at the database:

    图片.png-6.6kB

    If you don't like the discriminator column which entity framework auto create, you can define your column by adding these codes in OnModelCreating function of DbContext class:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    
        modelBuilder.Entity<Person>()
            .Map<InsidePerson>(p => p.Requires("PersonType").HasValue(1))
            .Map<OutsidePerson>(p => p.Requires("PersonType").HasValue(2));
    }
    

    Then, execute the command Add-Migration AddPesonTypeColumn2PeopleTable and Update-Database in nuget command line. Now, look at the database again:

    图片.png-6.5kB

    We can find the Entity Framework can't insert any value into PersonType column of existed rows. It's a little sad. Now, We insert some new data by coding:

    static void Main(string[] args)
    {
        using (MyDbContext db = new MyDbContext())
        {
            Person insidePerson4 = new InsidePerson()
            {
                PersonName = "InsidePerson4",
                Title = "Manager",
                Department = "development"
            };
            db.People.Add(insidePerson4);
    
            Person outsidePerson2 = new OutsidePerson()
            {
                PersonName = "outsidePerson2",
                CompanyName = "Baidu"
            };
            db.People.Add(outsidePerson2);
    
            db.SaveChanges();
        }
    }
    

    Look at the databas again:

    图片.png-9.1kB

    That's all.

  • 相关阅读:
    关于xcode 9.0数组问题的遇到的坑
    PHP将emoji表情进行过滤
    iOS 11更新后以及iPhone X推出后工程中遇到的问题及适配
    在IOS11中position:fixed弹出框中的input出现光标错位的问题
    采用腾讯云直播断流续播相关问题
    论坛灌水机与注册机问题
    以下内容对于灵活修改textField中文本以及占位文本属性进行了完整的封装,加入项目中可以节约开发时间。
    JS基础语法---总结
    JS基础语法---创建对象---三种方式创建对象:调用系统的构造函数;自定义构造函数;字面量的方式
    JS基础语法---编程思想和对象
  • 原文地址:https://www.cnblogs.com/zzy0471/p/6811269.html
Copyright © 2011-2022 走看看