实体框架代码优先铸造列
我有一个类
public class Foo
{
public int UserId { get; set; }
}
,我正在使用代码优先流畅映射将其映射到数据库。
Property(i => i.UserId)
.HasColumnName("userno");
唯一的问题是 userno 实际上是数据库中的 char(10) 。我该如何转换或转换这种类型?因为我目前收到此错误。
“Foo”的“UserId”属性无法设置为“String”值。 您必须将此属性设置为“Int32”类型的非空值。
谢谢
I have an class
public class Foo
{
public int UserId { get; set; }
}
and I'm using the code first fluent mapping to map this to the database.
Property(i => i.UserId)
.HasColumnName("userno");
the only problem is that userno is actually a char(10) in the database. How do I go about casting or converting this type? as I currently get this error.
The 'UserId' property on 'Foo' could not be set to a 'String' value.
You must set this property to a non-null value of type 'Int32'.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实体框架不支持映射中的类型转换,因此您的场景中唯一有效的映射属性是:
如果您还需要
int
属性,则必须执行以下操作:其添加到您的映射中:
并将 可以使用
UserId
属性的可访问性,但请注意,可访问性也会影响您的映射是否实际看到该属性。如果没有,您将根本不会映射UserId
。Entity framework doesn't have any support for type conversion in mapping so the only valid mapped property in your scenario is:
If you want
int
property as well you must do:And add this to your mapping:
You can play with accessibility of
UserId
property but be aware that accessibility also affects if your mapping actually sees the property. If it doesn't you will not haveUserId
mapped at all.