Entity Framework(五):使用配置伙伴创建数据库
在上一篇文章中讲了如何使用fluent API来创建数据表,不知道你有没有注意到一个问题。上面的OnModelCreating方法中,我们只配置了一个类Product,也许代码不是很多,但也不算很少,如果我们有1000个类怎么办?都写在这一个方法中肯定不好维护。EF提供了另一种方式来解决这个问题,那就是为每个实体类单独创建一个配置类。然后在OnModelCreating方法中调用这些配置伙伴类。
创建Product实体类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity.ModelConfiguration; namespace EF配置伙伴.Model
{
public class Product
{
public int ProductNo { get; set; } public string ProductName { get; set; } public double ProductPrice { get; set; }
}
}
创建Product实体类的配置类:ProductMap,配置类需要继承自EntityTypeConfiguration泛型类,EntityTypeConfiguration位于System.Data.Entity.ModelConfiguration命名空间下,ProductMap类如下:
using EF配置伙伴.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text; namespace EF配置伙伴.EDM
{
public class ProductMap :EntityTypeConfiguration<Product>
{
public ProductMap()
{
// 设置生成的表名称
ToTable("ProductConfiguration");
// 设置生成表的主键
this.HasKey(p => p.ProductNo);
// 修改生成的列名
this.Property(p =>p.ProductNo).HasColumnName("Id");
this.Property(p => p.ProductName)
.IsRequired() // 设置 ProductName列是必须的
.HasColumnName("Name"); // 将ProductName映射到数据表的Name列 }
}
}
在数据上下文Context类的OnModelCreating()方法中调用:
using EF配置伙伴.EDM;
using EF配置伙伴.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text; namespace EF配置伙伴.EFContext
{
public class Context:DbContext
{
public Context()
: base("DbConnection")
{ } public DbSet<Product> Products { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// 添加Product类的配置类
modelBuilder.Configurations.Add(new ProductMap());
base.OnModelCreating(modelBuilder);
} }
}
查看数据库,可以看到符合我们的更改:

这种写法和使用modelBuilder是几乎一样的,只不过这种方法更好组织处理多个实体。你可以看到上面的语法和写jQuery的链式编程一样,这种方式的链式写法就叫Fluent API。
Entity Framework(五):使用配置伙伴创建数据库的更多相关文章
- 使用Entity Framework通过code first方式创建数据库和数据表
开发环境 WIN10 Entity Framework6.0 MVC5.0 开发工具 VS2015 SqlServer2012 1.创建上下文Context继承DbContext,并创建其他的业 ...
- Entity Framework Codefirst的配置步骤
Entity Framework Codefirst的配置步骤: (1) 安装命令: install-package entityframework (2) 创建实体类,注意virtual关键字在导航 ...
- 关于Entity Framework采用DB First模式创建后的实体批量修改相关属性技巧
Entity Framework采用DB First模式创建实体是比较容易与方便的,修改已创建的实体在个数不多的情况下也是没问题的,但如果已创建的实体比较多,比如10个实体以上,涉及修改的地方比较多的 ...
- Entity Framework 关系约束配置
前言 简单的说一下自己的理解,大家应该都很明白ADO.NET,也就是原生态的数据库操作,直接通过拼接SQL语句,表与表之间通过链接(inner join left join 或者子查询),也就是在 ...
- centos 安装oracle 11g r2(二)-----监听配置与创建数据库实例
centos 安装oracle 11g r2(二)-----监听配置与创建数据库实例 一.监听配置(命令:netca) 1.以 oracle 用户输入命令,启动图形化工具配置监听 [oracle@lo ...
- Entity Framework学习笔记——配置EF
初次使用Entity Framework(以下简称EF),为了避免很快忘记,决定开日志记录学习过程和遇到的问题.因为项目比较小,只会用到EF的一些基本功能,因此先在此处制定一个学习目标:1. 配置EF ...
- Oracle 与 entity framework 6 的配置,文档
官方文档: http://docs.oracle.com/cd/E56485_01/win.121/e55744/intro001.htm#ODPNT123 Oracle 对 微软 实体框架 EF6 ...
- Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 创建复杂数据模型
Creating a complex data model 创建复杂数据模型 8 of 9 people found this helpful The Contoso University sampl ...
- EF CodeFirst 如何通过配置自动创建数据库<当模型改变时>
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 本篇为进阶篇,也是弥补自己之前没搞明白的地方,惭愧 ...
随机推荐
- [ES6] 05. The leg keyword -- 3. Block Scope
In ES6, IIFE is not necessary: // IIFE写法 (function () { var tmp = ...; ... }()); // 块级作用域写法 { let tm ...
- java基础知识精华
转载:https://www.jianshu.com/p/6c078abb720f java基础知识 java内存模型 java运行时数据区域 hashMap 如何解决冲突 存储方式 冲突达到一定数量 ...
- ShareSDK for Android 2.3.10已经公布
ShareSDK for Android 2.3.10已经公布,本次更新内容包含: 1.加入自己定义分享标签功能 新版本号SDK下载页面地址: http://share.sharesdk.cn/Dow ...
- UNIX网络编程读书笔记:套接口选项
概述 有很多方法来获取和设置影响套接口的选项: getsockopt和setsockopt函数 fcntl函数 ioctl函数 getsockopt和setsockopt函数 这两个函数仅用于套接口. ...
- uva10401Injured Queen Problem(递推)
题目:uva10401Injured Queen Problem(递推) 题目大意:依然是在棋盘上放皇后的问题,这些皇后是受伤的皇后,攻击范围缩小了.攻击范围在图中用阴影表示(题目).然后给出棋盘的现 ...
- Android调用OCR识别图像中的文字
// CharacterExtractor.java // Copyright (c) 2010 William Whitney // All rights reserved. // This sof ...
- java反射-获取方法信息
例子代码如下: package com.reflect; import java.lang.reflect.Method; public class ClassUtill { /* * 打印类的信息, ...
- 在CentOs6.5安装jdk
Linux CentOS 6.5 中安装与配置JDK-7:http://jingyan.baidu.com/article/fc07f9891d186512ffe51935.html jdk7的下载: ...
- Column count of mysql.user is wrong. Expected 43, found 42. Created with MySQL 50518, now running 50641. Please use mysql_upgrade to fix this error.
出现问题: Column count of mysql.user is wrong. Expected 43, found 42. Created with MySQL 50518, now runn ...
- perl: warning: Setting locale failed.
本篇文章由:http://xinpure.com/perl-warning-setting-locale-failed/ 将 mac 系统切换成英文后,使用 git 命令出现如下错误: perl: w ...