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.

转载于:https://www.cnblogs.com/zzy0471/p/6828956.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值