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:

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.

Lerning Entity Framework 6 ------ Defining the Database Structure的更多相关文章

  1. Lerning Entity Framework 6 ------ Defining Relationships

    There are three types of relationships in database. They are: One-to-Many One-to-One Many-to-Many Th ...

  2. Lerning Entity Framework 6 ------ Handling concurrency With SQL Server Database

    The default Way to handle concurrency of Entity Framework is using optimistic concurrency. When two ...

  3. 【Entity Framework】Revert the database to specified migration.

    本文涉及的相关问题,如果你的问题或需求有与下面所述相似之处,请阅读本文 [Entity Framework] Revert the database to specified migration. [ ...

  4. Lerning Entity Framework 6 ------ Working with in-memory data

    Sometimes, you need to find some data in an existing context instead of the database. By befault, En ...

  5. Lerning Entity Framework 6 ------ Inserting, Querying, Updating, and Deleting Data

    Creating Entities First of all, Let's create some entities to have a test. Create a project Add foll ...

  6. Lerning Entity Framework 6 ------ Introduction to TPH

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

  7. Entity Framework(EF)(一)之database first

    1.EF简介ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案.该框架曾经为.NET Framework的 ...

  8. Lerning Entity Framework 6 ------ Complex types

    Complex types are classes that map to a subset of columns of a table.They don't contains key. They a ...

  9. Lerning Entity Framework 6 ------ A demo of using Entity framework with MySql

    Create a new project named MySqlTest Install following packages by right-clicking on the References ...

随机推荐

  1. 【UI测试】--规范性

  2. EasyUI 分页 简洁代码

    做分页代码,看到网上很多人实现的方法,那是各种调用,各种获取对象.我很不解,因为Easyui已经给我们了分页的具体实现,为什么有些人要画蛇添足呢. 其实真正的分页,在你的代码中,别人可能都没有注意到, ...

  3. python中从内部循环直接跳出多层循环

    学习循环的时候碰到一道题,需要从内部循环中直接跳出所有循环,想了很久终于想到一种好办法(小白认知) 题目为:使用while循环输出100-50,从大到小,到50时,再循环输出0-50,从小到大. ex ...

  4. 选数(NOIP2002)

    题目链接:选数 这一题水过去就行了,我们这里用next_permutation去生成各种排列,有一个注意点,我会在代码中标注. #include<bits/stdc++.h> using ...

  5. 在ugui上显示3d物体

    1.接下来,使Cube的Layer和背景一样为UI层, 2.在将我们的主相机culling Mask改为UI,如果你还想渲染其他层的物体,可以根据需要该为需要的层,或者直接改为Everyting 3. ...

  6. 清幽傲竹实现的kbmMWServer数据库联接失败重联(转载红鱼儿)

    1.修改kbmMWUnidac单元的TkbmMWUNIDACConnection.InternalOpenConnection方法,加上:          //支持unidac重联          ...

  7. 将excel的数据导入到数据库后都乱码了是怎么回事

    将excel内容首先保存成csv格式,然后在MySQL数据库中导入,结果汉字出现了乱码. 解决过程: 1.csv文件以txt形式打开,另存为,选择utf-8编码. 2.数据库,设置,collation ...

  8. 开启Greenplum DataBase报错:could not bind IPv4 socket: Address already in use

    在运行gpstart时无法开启服务,查看日志看到下图所示错误: 查看日志错误大概是端口已被占用,所以无法重启. 解决方法: (1)利用ipcs查看数据库占用的共享内存.(如下图所示) (2)查看数据库 ...

  9. c#利用循环将类实例化为对象

    参考:https://yq.aliyun.com/wenzhang/show_6121 上面的代码每次使用前并没有实例化,会报错,实例化以下就好了,参考:http://bbs.csdn.net/top ...

  10. UVa 11542 Square (高斯消元)

    题意:给定 n 个数,从中选出一个,或者是多个,使得选出的整数的乘积是完全平方数,求一共有多少种选法,整数的素因子不大于 500. 析:从题目素因子不超过 500,就知道要把每个数进行分解.因为结果要 ...