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 ...
随机推荐
- 设置Android Studio启动时可选最近打开过的工程
Android Studio启动时,默认会打开最近关闭的工程. 如果想Android Studio在启动时,打开欢迎界面(Welcome to Android Studio界面),则可以通过设置Set ...
- 第一篇博客 用笨办法学python-14 提示和传递
# 代码如下: usr_name = input("")script = input("")prompt = '> 'print("hi %s, ...
- 分布式数据库中的Paxos 算法
分布式数据库中的Paxos 算法 http://baike.baidu.com/link?url=ChmfvtXRZQl7X1VmRU6ypsmZ4b4MbQX1pelw_VenRLnFpq7rMvY ...
- Jexus 支持PHP的三种方式
Jexus不仅支持ASP.NET,而且能够通个自带的PHP-FCGI服务以及PHP-FPM等方式灵活支持PHP而且还可以以.NET(Phalanger)方式支持PHP. PHP-FCGI服务支持PHP ...
- Linux服务器安装笔记 汇总
系统 Ubuntu笔记:http://www.cnblogs.com/newsea/p/4746967.html CentOs笔记:http://www.cnblogs.com/newsea/p/47 ...
- 表格搞定 Asp.net Web 状态管理
最近在网上搜罗了 ASP.NET WEB 状态管理方面的一些内容,终于把这些内容整合总结了一下. 1. 希望自己通过整理,能够掌握一些,为自己投资. 2. 以便自己忘记,又要浪费时间搜罗. 3. 希望 ...
- 你可能不知道的7个CSS单位
如果你是一名前端开发工程师,一般px和em使用频率比较高.但是今天本文的重点是介绍一些我们使用很少.甚至么有听说的单位. 一.重温em <style type="text/css&qu ...
- PDO 数据访问抽象层
1.操作其它数据库 (1)造对象 $dsn = "mysql:dbname=test3;host=localhost"; //数据源:两个参数:数据库驱动,链接数据库 $pdo = ...
- Rabbitmq安装与配置
install: 1.安装Erlang: $yum -y install erlang 2.安装rabbitmq-server: $rpm --import https://www.rabbitmq. ...
- Java Web应用的开发环境配置
1:第一是下载好Eclipse开发工具,这里不做叙述,自行下载安装. 2:使用Eclipse开发WEB项目,启动Eclipse,选择File--->new --->other---> ...