如何使用实体框架 4.1“仅代码"映射 char 属性?流畅的API? [英] How do I map a char property using the Entity Framework 4.1 "code only" fluent API?
问题描述
我有一个具有 char 属性的对象:
I have an object that has a char property:
public class Product
{
public char Code
{
get;
set;
}
}
Entity Framework 似乎无法映射字符(当我从模型对象创建数据库架构时,数据库中缺少此字段).无论如何我可以使用 fluent API 映射字符(例如到字符串)?我不想更改模型对象,因为它们是遗留共享库的一部分.
Entity Framework doesn't seem to be able to map chars (this field is missing from the database when I create the database schema from my model objects). Is there anyway I can map the char (e.g. to a string) using the fluent API? I don't want to change the model objects as they are part of a legacy shared library.
推荐答案
Char
is not valid original type for entity framework = entity framework doesn't map it.如果您检查 CSDL 参考,您将看到有效类型列表(char
不在其中).
Char
is not valid primitive type for entity framework = entity framework doesn't map it. If you check CSDL reference you will see list of valid types (char
is not among them).
数据库 char(1)
被翻译为 string
(SQL 到 CSDL 的翻译).Char
被描述为 固定长度为 1 的非 unicode 字符串.
Database char(1)
is translated as string
(SQL to CSDL translation). Char
is described as non-unicode string with fixed length 1.
唯一丑陋的选项是使用字符串的第二个映射属性,而您的 char
非映射属性将仅使用该属性中的 string[0]
.这只是 EF 中该死的缺少一些简单类型映射或转换器的另一个例子.
The only ugly option is second mapped property using string and your char
non-mapped property will just use string[0]
from that property. That is just another example how some simple type mapping or converters are damn missing in EF.
这篇关于如何使用实体框架 4.1“仅代码"映射 char 属性?流畅的API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!