zoukankan      html  css  js  c++  java
  • Code First 中的 TPH TPT TPC

          public class Blog
          {
                public int Id { get; set; }
                public DateTime Creationdate { get; set; }
                public string ShortDescription { get; set; }
                public string Title { get; set; }
                public string AboutTheAuthor { get; set; }
          }
    
          public class PictureBlog : Blog
          {
                public string PicDescription { get; set; }
          }
    
          public class VideoBlog : Blog
          {
                public string VideoDescription { get; set; }
          }

    配置 TPH 形成的数据表(一张表存放基类和子类的所有列,特定列区分):

    默认配置

    其中BlogType 可以修改的,如改成From,则这样:

    this.Map<Blog>(l => { l.Requires("From").HasValue("Blog"); });
    
    this.Map<Picture>(l => { l.Requires("From").HasValue("Pic"); });

    this.Map<Video>(l => { l.Requires("From").HasValue("Video"); });
     

    配置 TPT 形成的数据表(父类和子类在不同的表里,有主外键关系):

    this.Map(m => {

           m.ToTable("Blogs");

         }).Map<PictureBlog>(m =>{

                    m.ToTable("PictureBlogs");
    })
    .Map<VideoBlog>(m => {
                    m.ToTable("VideoBlogs");
    });

    配置 TPC 形成的数据表(子类包含父类的所有属性,各自独立):

    this.Map(m => {

           m.ToTable("Blogs");

         }).Map<PictureBlog>(m =>{

                    m.ToTable("PictureBlogs");
        m.MapInheritedProperties();
                }).Map<VideoBlog>(m => {
                    m.ToTable("VideoBlogs");
        m.MapInheritedProperties();
                });

  • 相关阅读:
    C# WinForm判断Win7下程序是否以管理员身份运行
    C#设置文件(夹)权限
    验证字符串是否为有效的IP地址
    内存对齐
    C#线程启动时传入参数
    写字符串内容到文件
    Thread.Abort() Is Evil.
    如何优雅地控制线程状态
    C#读取文件,返回字符串形式的文件内容
    关于产生不重复随机数的算法 C#
  • 原文地址:https://www.cnblogs.com/yipeng-yu/p/4418003.html
Copyright © 2011-2022 走看看