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. js 事件创建发布

    // 创建事件. var event = document.createEvent('Event'); // 初始化一个点击事件,可以冒泡,无法被取消 event.initEvent('click', ...

  2. php file_get_contents 使用3法

    <?php //GET function http_get($url, $params){ return file_get_contents($url.'?'.http_build_query( ...

  3. Windows-universal-samples学习笔记系列三:Navigation

    Navigation Back Button Master/detail Navigation menu (XAML) Pivot Projection XHR, handling navigatio ...

  4. python学习 day2 (3月2日)

    .if if else 和 if elif else 的区别是: 前者 判断第一个 判断完第二个 之后还会执行else: 后者是只有满足条件(即都不符合if.elif里的条件时才会进入else) 不清 ...

  5. 【转】Linux useradd

    Linux 系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统.用户的账号一方面可以帮助系统管理员对使用系统的用户进行 ...

  6. Oracle常用命令-用户、表空间、赋权限、导入导出

    1.1   删除表空间 drop tablespace QBKJ including contents and datafiles; 1.2   删除用户 drop user admin cascad ...

  7. android bug笔记

    昨天集成一个第三方的联盟sdk,然后执行它的代码,程序就会crash,这个第三方的sdk报错日志后面,紧跟着一个友盟分享的报错,我现在也不明白这里是为何把友盟给扯进来了,因为我这步骤操作上并没有调用任 ...

  8. ELASTIC SEARCH 性能调优

    ELASTICSEARCH 性能调优建议 创建索引调优 1.在创建索引的使用使用批量的方式导入到ES. 2.使用多线程的方式导入数据库. 3.增加默认刷新时间. 默认的刷新时间是1秒钟,这样会产生太多 ...

  9. SVN被锁定的几种解决方法

    用SVN经常出现被锁定而无法提交的问题,选择解锁又提示没有文件被锁定,很是头疼.这里整理了一下SVN被锁定的几种解决方法: 1.出现这个问题后使用“清理”即"Clean up"功能 ...

  10. DOM10-1节点层次

    DOM(问的那个对象模型)是针对HTML和XML文档的API.DOM描绘了一个层次化的节点树,允许开发人员添加.移除和修改页面的一部分. 每个节点都拥有各自的特点.数据和方法,另外和其他节点也存在某种 ...