实体框架代码首先铸造列
我有一个类
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");
唯一的问题是,userno实际上是在数据库中的CHAR(10)。我该如何去有关转换或转换这种类型的?因为我目前得到这个错误。
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.
在富的用户ID属性不能被设置为字符串值。
必须将此属性设置为类型'的Int32的非空值。
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'.
感谢
实体框架不具有映射类型转换任何支持所以在您的方案是唯一有效的对应的属性是:
Entity framework doesn't have any support for type conversion in mapping so the only valid mapped property in your scenario is:
public class Foo
{
public string UserId { get; set; }
}
如果你想 INT
属性以及你必须做的:
If you want int
property as well you must do:
public class Foo
{
public string UserId { get; set; }
public int UserIntId
{
get { return Int32.Parse(UserId); }
set { UserId = value.ToString(); }
}
}
和添加到您的映射:
Ignore(i => i.UserIntId);
您可以用用户ID
属性的辅助功能发挥但要知道,无障碍也会影响您的映射实际上看到的财产。如果没有你不会有用户ID
映射的。
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 have UserId
mapped at all.