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. powerdesigner mysql逆向工程注释不显示问题

  2. Windows 8.1 app 界面设计

    大纲: Windows 应用商店应用 UI 详细信息 http://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/dn263191.aspx 快 ...

  3. 【Java】JABX实现对象与XML互转

    JABX简介 JAXB能够使用Jackson对JAXB注解的支持实现(jackson-module-jaxb-annotations),既方便生成XML,也方便生成JSON,这样一来可以更好的标志可以 ...

  4. bootstrap 中 css 与 javascript 的使用

    1.css 1.1.选择器 1.2.子选择器: css 里的子元素用符号'>'表示.如下示例是表示拥有table样式的表盒,其thead元素内的tr元素如果有th的话,则应用该样式. .tabl ...

  5. AJAX-DOM事件

    1.DOM事件 1.select的onchange事件 当选项框中的内容发生改变时需要出发的事件.2.Ajax 1.名词解释 1.同步 在一个任务进行中,不能开启其它的任务. 同步访问:浏览器在向服务 ...

  6. canvas 实现时钟效果

    var clock = document.getElementById('clock'); var cxt = clock.getContext('2d'); function drawClock() ...

  7. 728. Self Dividing Numbers

    class Solution { public: vector<int> selfDividingNumbers(int left, int right) { vector<int& ...

  8. ios alloc init 和 new 的区别

    1.在实际开发中很少会用到new,一般创建对象咱们看到的全是[[className alloc] init] 但是并不意味着你不会接触到new,在一些代码中还是会看到[className new], ...

  9. 2018.10.26 NOIP训练 数数树(换根dp)

    传送门 换根dpdpdp傻逼题好像不好码啊. 考虑直接把每一个二进制位拆开处理. 先dfsdfsdfs出每个点到1的异或距离. 然后分类讨论一波: 如果一个点如果当前二进制位到根节点异或距离为1,那么 ...

  10. 下载安装mysql的一些坑

    在mysql下载安装的过程中会有一些坑,另外navicat连接mysql数据库时也存在一定的坑,总结如下: 1.计算机中丢失某个dll文件 这个问题好解决,下载个文件就搞定了,下载地址:https:/ ...