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:

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的更多相关文章
- 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 ...
- 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 ...
- 【Entity Framework】Revert the database to specified migration.
本文涉及的相关问题,如果你的问题或需求有与下面所述相似之处,请阅读本文 [Entity Framework] Revert the database to specified migration. [ ...
- 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 ...
- 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 ...
- Lerning Entity Framework 6 ------ Introduction to TPH
Sometimes, you have created two models. They have the same parent class like this: public class Pers ...
- Entity Framework(EF)(一)之database first
1.EF简介ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案.该框架曾经为.NET Framework的 ...
- 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 ...
- 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 ...
随机推荐
- java通过IP地址获取物理位置
import java.io.*; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern ...
- web项目目录结构
eclipse web项目目录结构 按照 Java EE 规范的规定,一个典型的 Web 应用程序有四个部分: 1. 公开目录 ; 2. WEB-INF/web.xml 文件,发布描述符(必选) ; ...
- SpringMVC环境搭建和详解
1.Spring容器和SpringMVC容器是父子容器 1.1 SpringMVC容器可以调用Spring容器中的所有内容 1.2 图示 2.SpringMVC环境搭建 1.导入jar包 2.在web ...
- AOP 和 前置通知,后置通知
Spring 1.AOP:中文名称面向切面编程 2.英文名称:(Aspect Oriented Programming) 3.正常程序执行流程都是纵向执行流程 3.1 又叫面向切面编程,在原有纵向执行 ...
- mysql mybatis useGeneratedKeys Field 'ID' doesn't have a default value的问题
原因是:创建表时没有让id自动增长: CREATE TABLE `STORAGE_VIRTUAL` ( `ID` ) NOT NULL AUTO_INCREMENT, `STORAGE_ID` ) N ...
- ubuntu卸载软件命令,apt-get remove
第一步,apt-get remove xxx :就是卸载xxx 或者 apt-get remove --purge xxx :卸载xxx并清除配置. 这两条命令对于依赖则是不管的.因为别的软件可 ...
- 【慕课网实战】Spark Streaming实时流处理项目实战笔记六之铭文升级版
铭文一级: 整合Flume和Kafka的综合使用 avro-memory-kafka.conf avro-memory-kafka.sources = avro-sourceavro-memory-k ...
- 开发.Net Script 模板-MyGeneration (翻译)
原文信息 原文地址 原文作者信息 Justin Greenwood MyGeneration Software http://www.mygenerationsoftware.com/ October ...
- Python 之自动获取公网IP
Python 之自动获取公网IP 2017年9月30日 文档下载:https://wenku.baidu.com/view/ff40aef7f021dd36a32d7375a417866fb84ac0 ...
- JS库汇总[重要]
库名称 说明 附加说明 URI 一款js 操作URL的插件 功能十分强大.可以增加 .修改.删除.参数 1.Animate.css 2.Hover.css 3.jQuery 4.WebUploa ...