Entity Framework Code First中有效的原始属性是什么? [英] What are valid primitive properties in Entity Framework Code First?
问题描述
当我尝试将列映射到模型类中的char数据类型时,出现错误:
When I try to map a column to a char data type in my model class I get an error:
属性'[ ColumnName]不是类型 [ClassName]的
声明属性。
使用忽略方法或
NotMappedAttribute数据批注,确认
未将属性
明确从模型中排除。
确保它是有效的原始
属性。
The property '[ColumnName]' is not a declared property on type '[ClassName]'. Verify that the property has not been explicitly excluded from the model by using the Ignore method or NotMappedAttribute data annotation. Make sure that it is a valid primitive property.
EF代码有效的原始类型是什么?首先?
What are the valid primitive types for EF Code First?
推荐答案
这很有趣,但您实际上无法映射 char
属性。我刚刚检查了一下,如果您想在数据库中包含 char(1)
,则必须在下面使用 string
属性映射:
This is interesting but you really cannot map char
property. I just checked it and if you want to have char(1)
in the database you must use string
property with following mapping:
modelBuilder.Entity<MyEntity>()
.Property(p => p.MyProperty)
.HasMaxLength(1)
.IsFixedLength()
.IsUnicode(false);
这不仅是代码优先的问题。这是整个EF的限制,因为EDMX设计器也不显示 char
类型。我认为允许的类型将与针对EDMX的CSDL参考中所述相同,因为代码首先只是包装器围绕相同的地图基础设施。
It is not only problem of Code-first. It is whole EF limitation because EDMX designer also doesn't show char
type. I think allowed types will be same as described in CSDL reference for EDMX because code first is just wrapper around the same mapping infrastructure.
这篇关于Entity Framework Code First中有效的原始属性是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!