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) 简单实体映 ...
随机推荐
- 一组带Tooltip提示框的CSS3图标按钮
在线演示 本地下载
- java套接字实现接口访问
是学校博客上的:http://blog.csdn.net/z69183787/article/details/17580325
- Java的TCP网络编程
服务端代码: package socket; import java.io.BufferedReader; import java.io.IOException; import java.io.Inp ...
- 基于Protobuf的分布式高性能RPC框架——Navi-Pbrpc
基于Protobuf的分布式高性能RPC框架——Navi-Pbrpc 二月 8, 2016 1 简介 Navi-pbrpc框架是一个高性能的远程调用RPC框架,使用netty4技术提供非阻塞.异步.全 ...
- Redis命令参考之复制(Replication)
Redis 支持简单且易用的主从复制(master-slave replication)功能, 该功能可以让从服务器(slave server)成为主服务器(master server)的精确复制品. ...
- 简单使用XStream
Stream1.什么作用 *可以把JavaBean转换为(序列化为)xml2.XStream的jar *核心jar:xstream-1.4.7.jar *必须依赖包:xpp3_min-1.1.4c(X ...
- c++的静态变量与静态函数
参考文献:静态成员函数和静态成员 一.静态变量: 1.静态变量属于类的变量,为类共享,在编译期间就分配好了内存. 2.静态变量在头文件中声明(和全局变量一样不要在头文件中定义静态变量),在.cpp文件 ...
- linux命令学习笔记(8):cp 命令
cp命令用来复制文件或者目录,是Linux系统中最常用的命令之一.一般情况下,shell会设置一个别名, 在命令行下复制文件时,如果目标文件已经存在,就会询问是否覆盖,不管你是否使用-i参数.但是如果 ...
- C++0X 学习之 auto
auto并不是一个新关键词,是一个把旧关键词赋予新的作用,新的作用修饰变量声明,指示编译器根据变量的初始化表达式推导变量应有的类型.auto 声明的变量必须“在声明处完成初始化”,编译器才可根据初始化 ...
- [: xxxx: Unexpected operator
/*************************************************************************** * [: xxxx: Unexpected o ...