【发布时间】:2013-03-07 09:39:06
【问题描述】:
我正在使用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 为假,1 为真,null 为空,任何其他数字为假。
更新
如果我将以上内容更改为:
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中将列类型设置为bit。
-
见我上面的更新。是的,我也想将它设置为 bit,但它不是我的服务器,所以我不能去更改表结构 :)
标签: entity-framework entity-framework-4 ado.net entity-framework-4.1 entity-framework-5