Enums and Lookup Tables with EF Code First
With EntityFramework’s support for enums, there is no longer any need to include lookup tables in the model. But I do want to have them in the database for integrity, even with code first.
I’ve been thinking for some time about to handle enums with code first. The idea behind code first is to be able to write the code as close as possible to how object oriented code is normally written. For enums that means that the enum definition itself is the constraint that is used to ensure that only valid values are used in the code.
For databases it would be possible to use a column constraint, but the normal way is to use a lookup table where the valid values are present. Any column in the database mapped against the enum type is then a foreign key to the lookup table to ensure integrity of the data.
What I would prefer is a solution where the lookup table is present in the database, but not mapped against any entity in the code.
Adding the Lookup Table
Adding the lookup table is simple when using Entity Framework Migrations. I’ve reused my old example on creating a database and updated Entity Framework to 6.0.1 that supports enums. When scaffolding a migration, theCarBodyStyle field (which is an enum) is now recognized by entity framework:
public override void Up() |
The lookup table and the foreign key can be added by extending the migration code:
public override void Up() |
The insert of the first value in CarBodyStyles is there to make sure that there is a matching value when the foreign key constraint is added to the Cars table. Without it, the foreign key can’t be added.
Adding Lookup Values
The migration takes care of adding the lookup table to the database, but that’s only the first half of the solution. The second half is to make sure that all the enum values are reflected in the lookup table automatically. The right place to do that is not in a migration, but in the Configuration.Seed() method that is called every time the migrations are applied. It is called even if there are no pending migrations.
protected override void Seed(TestLib.Entities.CarsContext context) |
That looks simple – doesn’t it? All of the work is deferred to the Seed<TEnum>() extension method. The lookup table isn’t mapped to the entity model, so there is no way to use all the nice features of entity framework for this operation. Instead I’m building up a plain Sql string, following the pattern from the Idempotent DB update Scripts post. I know that I’m not escaping data nor using parameterized Sql which leaves the risk för Sql injection. On the other hand I’m only using data from description attributes which is controlled by the developer so if you want to use it and Sql inject yourself – go ahead!
public static class EnumSeeder |
Enums and Lookup Tables with EF Code First的更多相关文章
- Using Lookup Tables to Accelerate Color Transformations
转自:http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter24.html In feature-film visual-effects ...
- Inheritance with EF Code First: Part 3 – Table per Concrete Type (TPC)
Inheritance with EF Code First: Part 3 – Table per Concrete Type (TPC) This is the third (and last) ...
- Inheritance with EF Code First: Part 2 – Table per Type (TPT)
In the previous blog post you saw that there are three different approaches to representing an inher ...
- Inheritance with EF Code First: Part 1 – Table per Hierarchy (TPH)
以下三篇文章是Entity Framework Code-First系列中第七回:Entity Framework Code-First(7):Inheritance Strategy 提到的三篇.这 ...
- EF Code First Migrations数据库迁移
1.EF Code First创建数据库 新建控制台应用程序Portal,通过程序包管理器控制台添加EntityFramework. 在程序包管理器控制台中执行以下语句,安装EntityFramewo ...
- EF Code First学习系列
EF Model First在实际工作中基本用不到,前段时间学了一下,大概的了解一下.现在开始学习Code First这种方式.这也是在实际工作中用到最多的方式. 下面先给出一些目录: 1.什么是Co ...
- EF和MVC系列文章导航:EF Code First、DbContext、MVC
对于之前一直使用webForm服务器控件.手写ado.net操作数据库的同学,突然来了EF和MVC,好多新概念泉涌而出,的确犹如当头一棒不知所措.本系列文章可以帮助新手入门并熟练使用EF和MVC,有了 ...
- EF Code First 初体验
Code First 顾名思义就是先代码,再由代码生成数据库的开发方式. 废话不多说,直接来一发看看:在VS2010里新建一个空白解决方案,再依次添加两个类库项目:Model.DataAccess和一 ...
- 【极力分享】[C#/.NET]Entity Framework(EF) Code First 多对多关系的实体增,删,改,查操作全程详细示例【转载自https://segmentfault.com/a/1190000004152660】
[C#/.NET]Entity Framework(EF) Code First 多对多关系的实体增,删,改,查操作全程详细示例 本文我们来学习一下在Entity Framework中使用Cont ...
随机推荐
- SQL Server 的动态语句(SQLServer 的String.format用法)(SQLServer的调用SQL占位符的使用)
直接上代码好了: --建表语句if exists(select 1 from [你的测试数据库名字].sys.tables where name='TDepartment') BEGIN print ...
- ASP.NET CORE下取IP地址
先记下来,以后用上了直接来这复制 string ip1 = HttpContext.Request.Headers["X-Real-IP"]; //取IP,NGINX中的配置里要写 ...
- Atitit 面试问题总结
Atitit 面试问题总结 1. 面试约人阶段可以预先1俩分钟大概问下情况1 2. 自我介绍阶段1 3. 技术方面2 3.1. 界面方面2 3.2. Java 本身 了解spring mybati ...
- ISE中的Force Process Up-to-Date功能:ISE中如何在未综合实现的前提下打开ChipScope ?
ISE中如何在未综合实现的前提下双击Analyze Design Using ChipScope打开ChipScope ? 有时,你正在ISE中调试程序,在ChipScope中看到了现象,顺手修改了程 ...
- # file Python-3.4.7.tar.xz Python-3.4.7.tar.xz: XZ compressed data
# file Python-3.4.7.tar.xz Python-3.4.7.tar.xz: XZ compressed data # xz -d Python-3.4.7.tar.xz # ls ...
- 如何高效的学习 TensorFlow 代码? 以及TensorFlow相关的论文
https://www.zhihu.com/question/41667903 源码分析 http://www.cnblogs.com/yao62995/p/5773578.html 如何贡献Tens ...
- 06-老马jQuery教程-jQuery高级
1.jQuery原型对象解密 jQuery里面的大部分API都是在jQuery的原型对象上定义的.jQuery源码中对原型对象做了简写的处理.也就是说:jQuery.fn === jQuery.pro ...
- mybatis中mapUnderscoreToCamelCase自动驼峰命名转换
ssm项目中在mybatis配置文件中添加以下配置,可以将数据库中user_name转化成userName与实体类属性对应,如果数据库使用如user_name的命名方式,实体类采用驼峰命名.配置后无需 ...
- C#学习笔记(17)——C#中接口的作用
说明(2017-7-17 23:50:48): 原文: 接口的作用 接口的作用(C#)(另一篇) C#接口是一个让很多初学C#者容易迷糊的东西,用起来好像很简单,定义接口,里面包含方法,但没有方法具体 ...
- WPF学习笔记(2)——动画效果按钮变长
说明(2017-6-12 11:26:48): 1. 视频教程里是把一个按钮点击一下,慢慢变长: 注意几个方面: (1)RoutedEvent="Button.Click",这里面 ...