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 ...
随机推荐
- 装箱问题(NOIP2001&水题测试2017082401)
题目链接:装箱问题 这题经典的01背包. 动规. 设计状态f[n][V]表示前n个物体放在V中的最大体积是多少. 所以代码如下: #include<bits/stdc++.h> using ...
- MZOJ 1134: 二叉苹果树
按书上大的,dfs还需加强 #include <bits/stdc++.h> #define read read() using namespace std; int read { ; c ...
- iOS知识基础篇 static
static关键字的作用 一.隐藏 通过static修饰的函数或者变量,在该文件中,所有位于这条语句之后的函数都可以访问,而其他文件中的方法和函数则不行: 二.静态变量 类方法不可以访问实例变量(函 ...
- 2019.02.07 bzoj1487: [HNOI2009]无归岛(仙人掌+树形dp)
传送门 人脑转化条件过后的题意简述:给你一个仙人掌求最大带权独立集. 思路:跟这题没啥变化好吗?再写一遍加深记忆吧. 就是把每个环提出来分别枚举环在图中的最高点选还是不选分别dpdpdp一下即可,时间 ...
- C++中1/0和1/0.0的区别
参考:https://zhidao.baidu.com/question/1494117716904764979.html 问题说明:在Dev中1/0会报错“除数不得为0”,但是1/0.0不报错,并且 ...
- GDI基础(3):绘制图片
1.CBitmap位图类封装了Windows GDI中的位图和操作位图的成员函数.CPen.CBrush.CFont.CBitmap是常用的Windows GDI对象,和CFont一样,CBitmap ...
- 无法重启oracle数据库监听
一.报错 TNS-12541: TNS:no listener TNS-12560: TNS:protocol adapter error TNS-00511: No listener Linu ...
- springboot+websocket示例
1.新建maven工程 工程结构如下: 完整的pom.xml如下: <?xml version="1.0" encoding="UTF-8"?> & ...
- mybatis xml中的大于、小于等符号写法
xml特殊符号转义写法 < < > > <> <> & & ' ...
- Oracle修改数据库的日期
---Oracle数据库更新时间字段数据时的sql语句---格式化时间插入 update t_invite_activityinfo set endtime=to_date('2019-10-30 1 ...