惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
L
LINUX DO - 热门话题
P
Proofpoint News Feed
P
Privacy & Cybersecurity Law Blog
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
Cisco Talos Blog
Cisco Talos Blog
AI
AI
L
LINUX DO - 最新话题
H
Heimdal Security Blog
Hacker News: Ask HN
Hacker News: Ask HN
Webroot Blog
Webroot Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The GitHub Blog
The GitHub Blog
I
Intezer
Blog — PlanetScale
Blog — PlanetScale
有赞技术团队
有赞技术团队
S
Securelist
博客园_首页
IT之家
IT之家
Schneier on Security
Schneier on Security
博客园 - 叶小钗
罗磊的独立博客
WordPress大学
WordPress大学
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
阮一峰的网络日志
阮一峰的网络日志
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
W
WeLiveSecurity
The Register - Security
The Register - Security
D
DataBreaches.Net
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
腾讯CDC
Recorded Future
Recorded Future
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tailwind CSS Blog
N
News and Events Feed by Topic
Cyberwarzone
Cyberwarzone
T
Tor Project blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com

博客园 - SOSOS's BLog

[转]视图多表 征懂IOS开发的同仁,联系QQ 755414 Web前端研发工程师 泛型数据生成Excel Unicode To ASCII,改写至Js ExtJs Extender Controls 3.2.0 截图及下载 多余的项目外包,可长期合作.深圳的朋友(已结束,谢谢支持) webservice小解 介绍一种查找网站被上传的恶意文件的方法 2G空间免费使用,只要你有站 08年又快结束了..抱怨下!~ 基础知识要牢固..复习复习,再复习 今天去面试.net开发,感想 淘宝"新版"首页 样式在.net下测试不成功.附解决办法 在b/s开发中经常用到的javaScript技术 学习.net2.0的网站 网站广告不再影响你网站速度的代码 Ajax技术简单入门
asp.net mvc3.0 在EF Code-First中自定义Model跟数据库中的表名、字段名的对应关系
SOSOS's BLog · 2012-11-23 · via 博客园 - SOSOS's BLog

一般情况下,我们的Model跟数据库中的表名、字段名都是一一对应的,如果我们需要结构有所不同呢?EF Code-First中提供自定义数据库结构的功能给我们了。我们可以通过重写DbSet中的OnModelCreating方法,去添加我们的Mapping信息。

1)数据库表明的映射
首先我们先来看看数据表明的映射。这里我需要将Departments映射到表名为tb_Departments的表上:

代码 复制 运行

protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder) { modelBuilder.Entity<Department>().MapSingleType().ToTable("tb_Departments"); }

2)字段的映射
我想在数据库中的每个字段中都会加上前缀“col_”,比如DepartmentID就是“col_DepartmentID”:

代码 复制 运行

protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder) { modelBuilder.Entity<Department>().MapSingleType(depart => new { col_DepartmentID = depart.DepartmentID, col_DepartmentName = depart.DepartName, col_Remark = depart.Remark }) .ToTable("tb_Departments"); }

3)复杂类型的映射
在这里我们添加一个CreateInfo的Model:

代码 复制 运行

public class CreateInfo { public string CreateUserID { get; set; } public DateTime? CreateDate { get; set; } }
我们Department信息中包含了一个CreateInfo类型的属性:

代码 复制 运行

public class Department { public int DepartmentID { get; set; } public string DepartName { get; set; } public string Remark { get; set; } public CreateInfo CreateInfo { get; set; } public virtual ICollection<Employee> Employees { get; set; } }
我们重写编写我们的Mapping信息:

代码 复制 运行

protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder) { modelBuilder.Entity<Department>().MapSingleType(depart => new { col_DepartmentID = depart.DepartmentID, col_DepartmentName = depart.DepartName, col_Remark = depart.Remark, col_CreateUserID= depart.CreateInfo.CreateUserID, col_CreateDate = depart.CreateInfo.CreateDate }) .ToTable("tb_Departments"); }
然后运行下我们发现会出现错误:System.NotSupportedException
原因是我们没有注册CreateInfo为complex 类型。所以我们首先需要注册CreateInfo为复合类型,在OnModelCreating中加上注册的代码:

代码 复制 运行

modelBuilder.ComplexType<CreateInfo>();

OK.