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 ...
随机推荐
- 20155312 实验一《Java开发环境的熟悉》实验报告
(一)命令行下Java程序开发 截图如下: 打印学号: 遇到的问题及 编译时如果还使用javac -d bin 文件名,就会出错 解决:package包是将编译文件放入指定包中 注意:代码中有pack ...
- python之函数篇3
一:函数的定义 1)函数的简单使用,无参函数 def f1(): # 定义函数指定函数名 print("hello") # 指定功能 f1() # 调用函数,才能执行函数体里面的功 ...
- mysql 初始密码、修改密码
新装MySQL,进不去,找不到网上说的什么临时密码,也没有见到放临时密码的文件,历经坎坷,终解决,,在此记录,谨防下次忘记,在此感谢原作者博文 系统 Ubuntu18.04 mysql Ver 14. ...
- apm飞行模式
参考 :https://www.cnblogs.com/jins-note/p/9580054.html 复制别人的,因为很久(几年)玩一次,所以会忘,也不好找,,若作者要求,请给留言,会立即删除 ...
- FMS是什么?
- c++关键字volatile的作用
1.易变性 1.1概念 编译器对volatile修饰的变量,当要读取这个变量时,任何情况下都会从内存中读取,而不会从寄存器缓存中读取(因为每次都从内存中读取体现出变量的“易变”) 1.2测试代码(VS ...
- c# DirectoryInfo 类和 FileInfo 类
1.DirectoryInfo 类 DirectoryInfo 类派生自 FileSystemInfo 类.它提供了各种用于创建.移动.浏览目录和子目录的方法.该类不能被继承. 2.FileInfo ...
- C++STL deque
deque双端数组 deque<int> dq; deque<int>::iterator it; dq.push_back();//尾部插入元素 dq.push_front( ...
- excel2007自定义菜单项学习
参考: http://club.excelhome.net/thread-1288002-1-1.html http://club.excelhome.net/thread-709306-1-1.ht ...
- 机器学习P7
优化问题: https://www.cnblogs.com/liaohuiqiang/p/7805954.html KKT条件就是把高数里面求不等式约束条件问题的分类方法写成两个条件.