zoukankan      html  css  js  c++  java
  • Lerning Entity Framework 6 ------ Defining the Database Structure


    There are three ways to define the database structure by Entity Framework API. They are:

    • Attributes
    • The DbModelBuilder API
    • Configuration Classes

    Attributes

    We can Add some attributes for entities or proteries to configure database structures. For example. Add some codes:

    [Table("People")]
    public class Person
    {
        [Key]
        public int PersonId { get; set; }
    
        [Required]
        [MaxLength(50)]
        public string Name { get; set; }
    
        [MaxLength(200)]
        public string Address { get; set; }
    }
    
    public class MyDb : DbContext
    {
        public MyDb():base("name=Test")
        {
    
        }
    
        public DbSet<Person> People { get; set; }
    }
    

    These attributes are in the namespace System.ComponentModel.DataAnnotations and System.ComponentModel.DataAnnotations.Schema.

    Then, execute the command Update-Database in Nuget command line. Now, look over the database:

    图片.png-4kB

    If you don't set any limit,the Entity Framework will also create the table successful, but maybe there are something you don't want. For example, if you remove the attribute MaxLength, the column's type will be longtext(I'm using MySql). You should take some attention to it.

    The DbModelBuilder API

    We also can use DbModelBuilder API to do it:

    public class MyDb : DbContext
    {
        public MyDb():base("name=Test")
        {
    
        }
    
        public DbSet<Person> People { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
    
            modelBuilder.Entity<Person>().ToTable("People");
    
            modelBuilder.Entity<Person>().Property(p => p.Name)
                .HasMaxLength(50)
                .IsRequired();
    
            modelBuilder.Entity<Person>().Property(p => p.Address)
                .HasMaxLength(200);
        }
    }
    

    Delete the table people first, then, execute the command Update-Database in Nuget command line and look over the database.

    Configuration Classes

    If we have too many entities, the class DbContext will contains too much codes. There is another way to define the database structure. You can Create a configuation class for each entities:

    public class PersonMap : EntityTypeConfiguration<Person>
    {
        public PersonMap()
        {
            ToTable("People");
            Property(p => p.Name).HasMaxLength(50).IsRequired();
            Property(p => p.Address).HasMaxLength(200);
        }
    }
    

    The namespace is System.Data.Entity.ModelConfiguration. Then, make DbContext some codes:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    
        modelBuilder.Configurations.Add(new PersonMap());
    }
    

    Delete the table people first, then, execute the command Update-Database in Nuget command line and look over the database.

    That's all.

  • 相关阅读:
    面试准备(集合部分)
    面试准备(算法部分)
    面试准备(sql部分 索引、常用语句 、)
    破解idea软件教程
    40个Java多线程问题详解复习
    面向对象(2)
    开发中容易造成内存泄露的操作
    面向对象(1)
    vue-cli中找不到jquery的原因,以使用ztree为例
    Django中整合Vue-cli,并解决各种路径引用错误和跨域的问题
  • 原文地址:https://www.cnblogs.com/zzy0471/p/6828956.html
Copyright © 2011-2022 走看看