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) 简单实体映 ...
随机推荐
- 跨平台(I版到K版)迁移实践总结
所谓跨平台迁移,对于了解openstack冷迁移过程的同学来说,其实就是手动去执行冷迁移的代码行为,当然像我这种抵制体力劳动的人,肯定会想写脚本去跑,即使不会也要边学边用. 迁移并非想象 ...
- Linux 中 bashrc 中的 rc 是什么意思
刚刚配置 .bashrc 文件时,突然想,这里面的 rc 是什么意思? 使用了这么长时间,这个都不知道,这是醉了.Google 之,还真有不少人探究过,哈哈. 这个 ubuntu 中文论坛里,有人回答 ...
- vim配置文件 .vimrc 重要参数
vim配置文件的路径为 ~/.vimrc 重要参数如下: set mouse=a //激活鼠标可用 syntax enable //开启语法 set cursorline //开启当前行光标线 ...
- web应用组成结构,web.xml的作用
- 大话设计模式--桥接模式 Bridge -- C++实现实例
1. 桥接模式: 将抽象部分与它的实现部分分离,使它们都可以独立的变化. 分离是指 抽象类和它的派生类用来实现自己的对象分离. 实现系统可以有多角度分类,每一种分类都有可能变化,那么把这种多角度分离出 ...
- JAVA- 数据库连接池原理
第一次Java程序要在MySQL中执行一条语句,那么就必须建立一个Connection对象,代表了与MySQL数据库的连接通过直接发送你要执行的SQL语句之后,就会调用Connection.close ...
- Codeforces 358D Dima and Hares:dp【只考虑相邻元素】
题目链接:http://codeforces.com/problemset/problem/358/D 题意: 有n个物品A[i]摆成一排,你要按照某一个顺序将它们全部取走. 其中,取走A[i]的收益 ...
- Spring4自动装配(default-autowire) (转)
原文地址:http://blog.csdn.net/conglinyu/article/details/63684957 Spring 自动装配 通过配置default-autowire 属性,Spr ...
- Jmete基础使用
1,jmeter下载与安装 Jmeter的运行需要JDK支持,所以需要先安装好jdk,并配置好环境变量: 下载地址:http://jmeter.apache.org/download_jmeter.c ...
- ffmpeg处理rtmp/文件/rtsp的推流和拉流
ffmpeg处理rtmp/文件/rtsp的推流和拉流 本demo演示了利用ffmpeg从服务器拉流或本地文件读取流,更改流url或文件类型名称发送回服务器或存到本地的作用. 由于本程序只写了3个小 ...