Configure Domain Classes in Code-First:

We learned default Code-First Conventions in the previous section. Code-First builds conceptual model from your domain classes using default conventions. Code-First leverages a programming pattern referred to as convention over configuration. It means you can override these conventions by configuring your domain classes to provide EF with the information it needs. There are two ways to configure your domain classes.

  1. DataAnnotations
  2. Fluent API

DataAnnotation:

DataAnnotation is a simple attribute based configuration, which you can apply to your domain classes and its properties. You can find most of the attributes in theSystem.ComponentModel.DataAnnotations namespace. However, DataAnnotation provides only a subset of Fluent API configurations. So, if you don't find some attributes in DataAnnotation, then you have to use Fluent API to configure it.

Following is an example of DataAnnotation used in Student Class:

[Table("StudentInfo")]
public class Student
{
public Student() { } [Key]
public int SID { get; set; } [Column("Name", TypeName="ntext")]
[MaxLength()]
public string StudentName { get; set; } [NotMapped]
public int? Age { get; set; } public int StdId { get; set; } [ForeignKey("StdId")]
public virtual Standard Standard { get; set; }
}

Fluent API:

Fluent API configuration is applied as EF builds the model from your domain classes You can inject the configurations by overriding the DbContext class' OnModelCreating method as following:

public class SchoolDBContext: DbContext
{
public SchoolDBContext(): base("SchoolDBConnectionString")
{
} public DbSet<Student> Students { get; set; }
public DbSet<Standard> Standards { get; set; }
public DbSet<StudentAddress> StudentAddress { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Configure domain classes using Fluent API here base.OnModelCreating(modelBuilder);
}
}

You can use modelBuilder, which is an object of DbModelBuilder class, to configure domain classes.

Let's see DataAnnotation and Fluent API in detail in the next chapter.

Entity Framework Code-First(8):Configure Domain Classes的更多相关文章

  1. Entity Framework Code first(转载)

    一.Entity Framework Code first(代码优先)使用过程 1.1Entity Framework 代码优先简介 不得不提Entity Framework Code First这个 ...

  2. Entity Framework Code First (三)Data Annotations

    Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表 ...

  3. Entity Framework Code First (二)Custom Conventions

    ---------------------------------------------------------------------------------------------------- ...

  4. Entity Framework Code First (一)Conventions

    Entity Framework 简言之就是一个ORM(Object-Relational Mapper)框架. Code First 使得你能够通过C#的类来描述一个模型,模型如何被发现/检测就是通 ...

  5. Entity Framework Tutorial Basics(11):Code First

    Code First development with Entity Framework: Entity Framework supports three different development ...

  6. Entity Framework Code First (七)空间数据类型 Spatial Data Types

    声明:本文针对 EF5+, Visual Studio 2012+ 空间数据类型(Spatial Data Types)是在 EF5 中引入的,空间数据类型表现有两种: Geography (地理学上 ...

  7. Entity Framework Code First (四)Fluent API - 配置属性/类型

    上篇博文说过当我们定义的类不能遵循约定(Conventions)的时候,Code First 提供了两种方式来配置你的类:DataAnnotations 和 Fluent API, 本文将关注 Flu ...

  8. Entity Framework Code First (八)迁移 Migrations

    创建初始模型和数据库 在开始使用迁移(Migrations)之前,我们需要一个 Project 和一个 Code First Model, 对于本文将使用典型的 Blog 和 Post 模型 创建一个 ...

  9. Entity Framework Code First (六)存储过程

    声明:本文只针对 EF6+ 默认情况下,Code First 对实体进行插入.更新.删除操作是直接在表上进行的,从 EF6 开始你可以选择使用存储过程(Stored Procedures) 简单实体映 ...

随机推荐

  1. 对类型化数组(Typed Array)与ArrayBuffer的理解 转囧囧

    类型化数组(Typed Array)也是HTML5中新引入的API.用一句话解释类型化数组就是:它是JS操作二进制数据的接口. 众所周知,直接操作二进制数据可以使程序更为高效, 尽管JS对常规数组做了 ...

  2. 剑指offer——求1+2+...+n

    方法一.通过在类的构造函数中执行加的过程. #include <iostream> using namespace std; class Base { public: Base(){n++ ...

  3. while 读取文件内容

    exec < filename while read line;do echo $line done 方法1 while read line;do echo $line done<$ 方法 ...

  4. Oracle结构控制语句

    --if语句 if [判断条件] then --条件满足执行的语句 end if; -- if ...else... if [判断条件] then ----条件满足执行的语句 else --不满足条件 ...

  5. 现有exe转为服务_方式01

    1.安装X.exe服务: ...>路径\X.exe /install 2.卸载X.exe服务: ...>路径\X.exe /uninstall 3.开始运行XX(程序是X.exe,服务名是 ...

  6. 仿联想商城laravel实战---7、lavarel中如何给用户发送邮件

    仿联想商城laravel实战---7.lavarel中如何给用户发送邮件 一.总结 一句话总结: 设置邮件服务器,比如163邮箱 lavarel中配置邮件服务,在.env中 控制器中使用Mail对象发 ...

  7. Sqlte 知识点记录

    1.表存在 select count(*) from sqlite_master where type='table' and name='MyTable'; sql),path ))"; ...

  8. SetOperations

    无序集合,add的顺序不是存储顺序 1.add(K key, V value) 2.difference(K key, otherK[s]) :差集,返回Set 3.differenceAndStor ...

  9. Java集合类--->入门下篇

    HashSet集合 在上篇大概了解了什么是集合类,知道它可以存储任意类型的对象,并且比数组灵活,集合类的长度可以变化.这里将接着介绍一下,Set接口的实现类之一,HashSet集合,Set集合:元素不 ...

  10. vc6++Release和Debug

    1. 如何快速地规范代码缩进格式 选中所需要规范的代码,按shift+F8 2. 如何在Release状态下进行调试 Project->Setting=>ProjectSetting对话框 ...