6.Configure Domain Classes(配置领域类)【EF Code-First 系列】
在前面的部分中,我们学习了Code-First默认约定,Code-First使用默认的约定,根据你的领域类,然后生成概念模型。
Code-First模式,发起了一种编程模式:约定大于配置。这也就是说,当你需要的时候,你可以重写这些约定,通过配置你的领域类。这里有两种方式来配置你的领域类实体:
- DataAnnotations(数据注解)
- Fluent API(姑且翻译为:流畅API)
数据注解:
数据注解是基于配置的简单特性,你可以应用到你的领域类或者其属性中。你可能会发现大多数的特性都在这个命令空间下面:
System.ComponentModel.DataAnnotations, 然而,数据注解提供的特性仅仅是Fluent API配置的一部分子集而已,所以,如果你,在数据注解中,没有找到的属性,可以使用Fluent API来配置。
下面的例子中,是一个简单使用数据注解的例子:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF2
{
[Table("StudentInfo")]
public class 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配置,利用EF从你的实体类中构建模型,你可以注入这个配置,通过重写DbContext类的“OnModelCreating”方法,例如下面的:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF2
{
public class DbContextClass:DbContext
{
public DbContextClass() : base("Constr") { }
public DbSet<Student> Students { get; set; }
public DbSet<Standard> Standards { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{ //这里面配置领域类实体,通过使用Dluent API
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DbContextClass>());
base.OnModelCreating(modelBuilder);
}
}
}
You can use modelBuilder, which is an object of DbModelBuilder class, to configure domain classes.
你可以使用modelBuider,他是DBModelBuilder类的对象,用来配置领域类的。
Let's see DataAnnotation and Fluent API in detail in the next chapter.
我们在后面的章节中将会详细的了解数据注解和Fluent API。
6.Configure Domain Classes(配置领域类)【EF Code-First 系列】的更多相关文章
- 8.翻译系列: EF 6中配置领域类(EF 6 Code-First 系列)
原文地址:http://www.entityframeworktutorial.net/code-first/configure-classes-in-code-first.aspx EF 6 Cod ...
- EF CodeFirst配置领域类
当我们不想使用EF的默认约定时,可以手动配置领域类,但还是推荐少配置,Simple is best! 两种配置方式: 1.Data Annotation Attributes[数据注解特性] 数据注 ...
- Entity Framework Code-First(8):Configure Domain Classes
Configure Domain Classes in Code-First: We learned default Code-First Conventions in the previous se ...
- 9.Configure One-to-One(配置一对一关系)【Code-First系列】
现在,开始学习怎么配置一对一的关系,众所周知,一对一的关系是:一个表中的主键,在另外一个表中,同时是主键和外键[实际上是一对零或者一对一]. 请注意:一对一的关系,在MS SQL Server中,技术 ...
- 1.什么是Code First(EF Code First 系列)
EF4.1中开始支持Code First .这种方式在领域设计模式中非常有用.使用Code First模式,你可以专注于领域设计,根据需要,为你一个领域的对象创建类集合,而不是首先来设计数据库,然后来 ...
- EntityFramework Code-First 简易教程(五)-------领域类配置
前言:在前篇中,总是把领域类(Domain Class)翻译成模型类,因为我的理解它就是一个现实对象的抽象模型,不知道对不对.以防止将来可能的歧义,这篇开始还是直接对Domain Class直译. 前 ...
- EntityFramework Code-First 简易教程(七)-------领域类配置之Fluent API
Fluent API配置: 前面我们已经了解到使用DataAnotations特性来覆写Code-First默认约定,现在我们来学习Fluent API. Fluent API是另一种配置领域类的方法 ...
- 【译】第8节---EF Code First中配置类
原文:http://www.entityframeworktutorial.net/code-first/configure-classes-in-code-first.aspx 前面的章节中我们知道 ...
- 17.翻译系列:将Fluent API的配置迁移到单独的类中【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/move-configurations-to-seperate-class-in-cod ...
随机推荐
- 如何在Windows 2003+IIS6的环境下找回应用程序池(application pool)中的服务账号密码
上一篇文章说了说如何在Win2008+iis7中取出SharePoint管理账号密码的方法. 整个过程简单的讲,就是通过使用要找回密码的账号用来在SharePoint中创建一个临时的Web Appli ...
- IOS网络第七天WebView-02WebView和网页的交互2,删除大众点评多余文字,加上蒙版进度
************ #import "HMViewController.h" @interface HMViewController () <UIWebViewDele ...
- 使用libjpeg.framework压缩UIImage
+(void)writeFile:(NSString *)filePath withQuality:(int)quality { //初始化图片参数 UIImage *image=[UIImage i ...
- CodeFirst实战:用文本数据库存档软件配置
背景: 以前要写软件的时候,在编写用户配置这一块时,由于存档数据库不靠谱或大题小作,所以一般是存在文本中. 一开始是一个文件保存一个配置(图个File.Read与File.Write的操作简单) 由于 ...
- 性能计数器与profiler的组合性能诊断
性能计数器和sql profiler都是常用的性能诊断工具和优化工具,最近和群友聊天发现很多人竟然不知道这两个可以“组合”使用,所以这篇算是一篇扫盲贴吧. 两种工具简述 通过计数器可以收集两部分内容: ...
- jQuery源码分析系列(37) : Ajax 总结
综合前面的分析,我们总结如下3大块: jQuery1.5以后,AJAX模块提供了三个新的方法用于管理.扩展AJAX请求 前置过滤器 jQuery. ajaxPrefilter 请求分发器 jQuery ...
- WPF自定义控件与样式(2)-自定义按钮FButton
一.前言.效果图 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 还是先看看效果 ...
- 自制编程语言crowbar(v0.1)构建解析器时分配内存
crowbar中第一次申请内存是在生成解析器的时候: /* interface.c */CRB_Interpreter *CRB_create_interpreter(void) { MEM_Stor ...
- Android数据存储之Sqlite的介绍及使用
前言: 本来没有打算整理有关Sqlite数据库文章的,最近一直在研究ContentProvider的使用,所有觉得还是先对Sqlite进行一个简单的回顾,也方便研究学习ContentProvider. ...
- 辨析relative与absolute
谈起它们,想必大家都不陌生.relative,相对定位嘛:absolute,绝对定位嘛.但是它们到底是个啥东东呢? 看看w3c的定义,见下表 定位 含义 relative 元素框偏移某个距离.元素仍保 ...