我正在使用Entity Framework 5 code first
。我的表有一个名为Active
的列,其数据类型为int
。存储在Active中的值是0
,1
和null
。实体框架代码首先在类布尔和列整数之间转换
我有一个类,我需要映射到这张表。
public class CommandExecutionServer : IEntity
{
public int Id { get; set; }
public bool? IsActive { get; set; }
}
这是我的配置文件。我试图将我的类的布尔值属性映射到数据库中的整数字段。
class CommandExecutionServerConfiguration : EntityTypeConfiguration<CommandExecutionServer>
{
internal CommandExecutionServerConfiguration()
{
this.ToTable("tblCommandExecutionServers");
this.Property(x => x.IsActive).HasColumnName("Active").HasColumnType("bit");
}
}
这是行不通的。我正的错误是:
The 'IsActive' property on 'CommandExecutionServer' could not be set to a 'Int32' value. You must set this property to a non-null value of type 'Boolean'
我尝试添加.HasColumnType("bit")
并认为这可能需要我的问题。我该怎么做呢?理想情况下,我希望0为false,1为true,null为null,其他任何数字为false。
UPDATE
如果我改变了上面:
this.Property(x => x.IsActive).HasColumnName("Active").HasColumnType("int");
...然后我得到以下错误:
Member Mapping specified is not valid. The type 'Edm.Boolean[Nullable=True,DefaultValue=]' of member 'IsActive' in type 'MyProject.Infrastructure.EntityFramework.CommandExecutionServer' is not compatible with 'SqlServer.int[Nullable=True,DefaultValue=]' of member 'Active' in type 'CodeFirstDatabaseSchema.CommandExecutionServer'.
你试过用HasColumnType(“int”)?在同样的问题中,我将列类型设置为在SQL Server中位。 – tschmit007 2013-03-07 09:52:04
看到我上面的更新。是的,我也想将它设置为位,但它不是我的服务器,所以我不能去改变表结构:) – 2013-03-07 10:15:24