Entity Framework Code-First(8):Configure Domain Classes
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.
- DataAnnotations
- 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的更多相关文章
- Entity Framework Code first(转载)
一.Entity Framework Code first(代码优先)使用过程 1.1Entity Framework 代码优先简介 不得不提Entity Framework Code First这个 ...
- Entity Framework Code First (三)Data Annotations
Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表 ...
- Entity Framework Code First (二)Custom Conventions
---------------------------------------------------------------------------------------------------- ...
- Entity Framework Code First (一)Conventions
Entity Framework 简言之就是一个ORM(Object-Relational Mapper)框架. Code First 使得你能够通过C#的类来描述一个模型,模型如何被发现/检测就是通 ...
- Entity Framework Tutorial Basics(11):Code First
Code First development with Entity Framework: Entity Framework supports three different development ...
- Entity Framework Code First (七)空间数据类型 Spatial Data Types
声明:本文针对 EF5+, Visual Studio 2012+ 空间数据类型(Spatial Data Types)是在 EF5 中引入的,空间数据类型表现有两种: Geography (地理学上 ...
- Entity Framework Code First (四)Fluent API - 配置属性/类型
上篇博文说过当我们定义的类不能遵循约定(Conventions)的时候,Code First 提供了两种方式来配置你的类:DataAnnotations 和 Fluent API, 本文将关注 Flu ...
- Entity Framework Code First (八)迁移 Migrations
创建初始模型和数据库 在开始使用迁移(Migrations)之前,我们需要一个 Project 和一个 Code First Model, 对于本文将使用典型的 Blog 和 Post 模型 创建一个 ...
- Entity Framework Code First (六)存储过程
声明:本文只针对 EF6+ 默认情况下,Code First 对实体进行插入.更新.删除操作是直接在表上进行的,从 EF6 开始你可以选择使用存储过程(Stored Procedures) 简单实体映 ...
随机推荐
- webpack打包笔记
optimist是一个node库,将webpack.config.js与shell参数整合成options对象 options对象包含之后构建的重要信息,类似于webpack.config.js we ...
- web框架详解之tornado 三 url和分页
一.代码目录构建 controllers :处理业务逻辑的 account:处理账户相关的 上面目录作用和内容 controllers 包 :处理业务逻辑的 account:处理账户相关的 home ...
- DEDE 列表页调用如 标题,时间,简介等
以下是直接从板子中复制出来的,CSS自已根据需要写下就行.在调时简介长度不知道怎么控制,现在说下方法1. infolen='30' 这个可以2. 系统设置 >其它设置 >内容简介长度填下就 ...
- 希尔排序(Shell Sort)
一.思路 希尔排序是基于插入排序算法,通过允许不相邻的元素进行交换这一简单的改进,使数组变为局部有序,最终再用插入排序. 希尔排序的思想是使数组中任意间隔h的元素都是有序的.这样的数组被称为h有序数组 ...
- pyqt5开发之俄罗斯方块
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial This is a Tetri ...
- string(未完待续)
1.string字符串的长度 可以用 a.length() 来测,或者是a.size() 来测 不可以用strlen(a)来求其长度, sizeof(a)是固定值16, 求的是strin ...
- Django 模版当中使用中文 UnicodeDecodeError at / 问题
Django 再次是当中字符编码问题 今天使用了bootstrap 当中的一些CSS 对自己的博客项目当中的一些东西进行美化 但是很奇怪的是 当 诸如按钮类的加入 中文字符后 就会提示 Unicode ...
- leetcode 50. Pow(x, n)(快速幂)
就是一个二分法快速幂. 但是需要注意的问题是这里是实数,而且n可能为负.int的范围是-2,147,483,648 至 2,147,483,647.如果为-2,147,483,648那么直接n=-n就 ...
- SPOJ8093Sevenk Love Oimaster(广义后缀自动机)
Oimaster and sevenk love each other. But recently,sevenk heard that a girl named ChuYuXun was da ...
- LeetCode:Add Digits - 非负整数各位相加
1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits/ 3.题目内容 英文:Given a non- ...