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

推荐订阅源

美团技术团队
D
DataBreaches.Net
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Check Point Blog
腾讯CDC
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
IT之家
IT之家
月光博客
月光博客
U
Unit 42
K
Kaspersky official blog
T
Threatpost
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
GbyAI
GbyAI
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
云风的 BLOG
云风的 BLOG
酷 壳 – CoolShell
酷 壳 – CoolShell
I
InfoQ
Engineering at Meta
Engineering at Meta
Recorded Future
Recorded Future
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security @ Cisco Blogs
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
Security Archives - TechRepublic
Security Archives - TechRepublic
Webroot Blog
Webroot Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Schneier on Security
S
Secure Thoughts
The Register - Security
The Register - Security
B
Blog RSS Feed
The Last Watchdog
The Last Watchdog
P
Palo Alto Networks Blog
爱范儿
爱范儿
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
L
LINUX DO - 热门话题
C
Cisco Blogs
Spread Privacy
Spread Privacy
F
Full Disclosure
博客园 - 聂微东
T
The Blog of Author Tim Ferriss

博客园 - 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.