Sidney
Sidney

Reputation: 85

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

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

Upvotes: 1

Views: 1887

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364279

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.

Upvotes: 1

Related Questions