Is it possible to create User-Defined Data Types when using EF 4.1 Code First?

谁说胖子不能爱 提交于 2019-12-10 11:41:32

问题


When using EF 4.1 Code First, is it possible to create User-Defined Data Types for your schema?


回答1:


Simple answer is no.

Longer answer:

Current EF implementation leads to multiple issues when trying to use user defined types:

  • The type must be defined prior to its usage in table's DDL definition. Because of that the type cannot be defined in Seed method of database initializer (as often used for other database constructs like triggers or indexes). To make this work you must create whole new initializer by implementing IDatabaseInitializer and separate database creation and table creation because custom type definitions must be between them. Here is some example how to create database initializer (this one will recreate tables every time you run the application).
  • Creating database initializer which reflects model changes from sketch is more complicated because this logic is probably internal to EF
  • You must also add check that type is not already created
  • Even if you have initializer you still face the biggest blocker. EF is not ready to allow you defining user defined types for columns - neither ColumnAttribute and HasColumnType in fluent API will accept custom type. So your mapping must specify basic primitive SQL types and these custom types will be used in tables DDL produced by EF. So unless custom database initializer post-process those generated SQL scripts and replaces basic types with custom types (really ugly solution) tables will not use them.


来源:https://stackoverflow.com/questions/8087661/is-it-possible-to-create-user-defined-data-types-when-using-ef-4-1-code-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!