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. 《Django By Example》第一章 学习笔记

    首先看了下目录,在这章里 将会学到 安装Django并创建你的第一个项目 设计模型(models)并且生成模型(model)数据库迁移 给你的模型(models)创建一个管理站点 使用查询集(Quer ...

  2. tensorflow下识别手写数字基于MLP网络

    # coding: utf-8 # In[1]: import tensorflow as tf import tensorflow.examples.tutorials.mnist.input_da ...

  3. python 中 __name__ 的使用

    1. 如果模块是被导入,__name__的值为模块名字2. 如果模块是被直接执行,__name__的值为’__main__’   Py1.py #!/usr/bin/env python def te ...

  4. 证明抛物线焦点发出的光线经y=ax^2反射后平行于y轴

  5. latex去掉页眉

    \begin{document}之前添加 \fancyhead{} 即: \fancyhead{} \begin{document}

  6. 研究生flag

    是时候定个计划了,感觉日子一天天水,不加油学点东西,迟早要掉队…… 刷刷算法题库吧,貌似选几个管用的刷刷——https://hihocoder.com/problemset 争取明年三月份的PAT顶级 ...

  7. php 验证码 图像存在错误 无法显示 解决方法

    <?php $height = 300; $width = 300; $im = imagecreatetruecolor($width, $height); $white = imagecol ...

  8. ArcGIS API for Silverlight部署本地地图服务

    这一节我们来讲新建立的ArcGIS API for Silverlight应用程序如何加载自己的地图服务的问题,网上的资料讲的都有点含糊不清,这次我们详细的讲一下配置的步骤: 首先介绍下我们的开发和部 ...

  9. PreTranslateMessage(MSG* pMsg)专题

    .. BOOL CQuickMosaicDlg::PreTranslateMessage(MSG* pMsg) { if (pMsg->message==WM_KEYDOWN) //键盘按下 { ...

  10. Write Markdown Syntax Online Document with Sphinx and Pandoc

    There is no doubt that we have to write doc while we are developing software. But How do you write d ...