是否有可能创建用户定义的数据类型使用C首先EF 4.1 $ C $的时候?有可能、数据类型、定义、时候

2023-09-03 04:29:52 作者:苏姬

在使用EF 4.1 code首先,是否有可能创建用户定义的数据类型的模式?

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

推荐答案

简单的答案是否定的。

再回应:

尝试使用用户定义类型时,当前EF实施会导致多种问题:

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

的类型必须先于它的使用表的DDL定义中定义。正因为如此,该类型不能在数据库初始化的种子方法定义(如经常用于其他的数据库结构,如触发器或索引)。为了使这项工作,你必须创建通过实施全新的初始化 IDatabaseInitializer 和独立的数据库创建和表创建,因为自定义类型定义,必须是两者之间。 这里是一些例子,如何创建数据库的初始化(这个每次运行应用程序时,将重新创建表)。 在创建数据库初始化它反映了模式的转变,从草图是比较复杂的,因为这种逻辑可能是内部EF 尚未创建您还必须添加检查类型 即使你有初始化你仍然面临的最大拦截。 EF还没有准备好让你定义用户定义类型的列 - 既不 Col​​umnAttribute HasColumnType 用流利的API将接受定制类型。所以,你的映射,必须指定基本的原始SQL类型与这些自定义类型将用于表的DDL由EF生产。所以,除非自定义数据库初始化后处理这些生成的SQL脚本,并取代基本类型与自定义类型(真正丑陋的解决方案)表中不会使用它们。 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.