10 EF Core inherited class relational mapping

1, the case of agreement, may be included in the model type is disclosed in an inheritance hierarchy for each type by DbSet

class MyContext: the DbContext 
{ 
    public DbSet <Blog> Blogs { GET ; SET ;} // convention specifies the database tables to map
     public DbSet <RssBlog> RssBlogs { GET ; SET ;} // convention specifies the mapping table to the database 
} 

public  class Blog 
{ 
    public  int blogid { GET ; SET ;}
     public  String the Url { GET ; SET ;} 
} 
// inherited class 
public  class RssBlog: Blog 
{ 
    public  String RssUrl {get; set; }
}

Fluent API configuration may be used when Blog, RssBlog model name of the base class mapping table in the database with a list of model types with different data field identifies Discriminator, if you want to replace Discriminator field called other names

 

 Automatically set as required database column model no database model according to the type of field type is null

 

2, HasBaseTypeexplicitly specify the type of group
If the agreement is not dependent on the configuration MyContext type public DbSet <Blog> Blogs {getproperty may be used  HasBaseTypeto explicitly specify the type of group
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<RssBlog>().HasBaseType<Blog>();
            base.OnModelCreating(modelBuilder);
        }

 

3, inheriting the base class type, but when you want to map databases, different type map, use  HasBaseType((Type)null) delete an entity type from the hierarchy

public  class BloggingContext: the DbContext 
{ 
     protected  the override  void OnModelCreating (the ModelBuilder modelBuilder) 
        { 
            modelBuilder.Entity <RssBlog> () 
        .HasBaseType ((the Type) null ) // set no base class, in the database are mapped to different tables 
        .HasKey ( RB => rb.BlogId);   // if the derived class is not defined in a primary key, then the primary key provided base class is the primary key class inheritance, 
            base .OnModelCreating (modelBuilder); 
        } 
} 
public  class Blog 
{ 
    public  int blogid { GET ; sET ;}
     public String the Url { GET ; SET ;} 
} 
// inherited class 
public  class RssBlog: Blog 
{ 
    public  String RssUrl { GET ; SET ;} 
}

 

4, Discriminator column configuration

protected  the override  void OnModelCreating (the ModelBuilder modelBuilder) 
{ 
    modelBuilder.Entity <Blog> () 
        .HasDiscriminator < String > ( "BlogType " ) // set the data type of the column identification string, the column named:. BlogType data type can be string, int 
        .HasValue <Blog> ( "B log_Base " ) // insert a specified database data, different data type identification value. Blog insertion type data, BlogType column values: BlogBase 
        .HasValue <RssBlog> ( " Blog_RSS " ); 

modelBuilder.Entity <Blog> ()
  .HasDiscriminator <int> ( "BlogType") // set to type int BlogType
  When .HasValue <Blog> (0) // Blog insertion type data, when the value corresponding BlogType 0
  .HasValue <RssBlog> (. 1)
  .HasValue <XmlBlog> (2);

}

 

 



Guess you like

Origin www.cnblogs.com/Adoni/p/12329796.html