实体框架code一流的布尔和列的整数之间的转换 [英] Entity framework code first convert between class boolean and column integer
问题描述
我使用实体框架5 code首先
。我的表中有一个名为列活动
和其数据类型为类型 INT
。存储在Active的值 0
, 1
和空
。
我有我需要映射到该表中的一类。
公共类CommandExecutionServer:IEntity
{
公众诠释编号{获得;组; }
公共BOOL? IsActive {获得;组; }
}
下面是我的配置文件。我想在我的课地图我布尔属性的整型字段在数据库中。
类CommandExecutionServerConfiguration:EntityTypeConfiguration< CommandExecutionServer>
{
内部CommandExecutionServerConfiguration()
{
this.ToTable(tblCommandExecutionServers);
this.Property(X => x.IsActive).HasColumnName(激活)HasColumnType(位)。
}
}
这都不尽如人意。那我得到的错误是:
在CommandExecutionServer的IsActive属性不能被设置为的Int32的价值。您必须将此属性设置为类型的非空值布尔
我尝试添加 .HasColumnType(位)
,认为它可能需要我的问题。我该怎么做呢?理想情况下,我想0为假,1为真,空为空,而任何其他数字为false。
更新
如果我上面的改变:
this.Property(X => x.IsActive).HasColumnName(激活)HasColumnType(INT)。
...然后我得到了以下错误:
指定 会员映射无效。类型Edm.Boolean [可空=真,默认值='类型成员IsActive的MyProject.Infrastructure.EntityFramework.CommandExecutionServer'是不符合SqlServer.int [可空=真,默认值='成员的兼容有效类型codeFirstDatabaseSchema.CommandExecutionServer。
我尝试以下,因为我不知道,如果实体框架能够处理的转换对我来说。
我删除了这一行:
this.Property(X => x.IsActive).HasColumnName(激活)HasColumnType(INT)。
我加到一个属性来我 CommandExecutionServer类
:
公共类CommandExecutionServer:IEntity
{
公众诠释编号{获得;组; }
公众诠释?活动{获得;组; }
公共布尔IsActive
{
得到
{
(主动== 1)?真假;
}
}
}
有可能是一个更好的办法,但这对我的作品现在。如果任何一个可以更好的这个话,请继续:)
I am using Entity Framework 5 code first
. My table has a column called Active
and its datatype is of type int
. The values that are stored in Active are 0
, 1
and null
.
I have a class that I need to map to this table.
public class CommandExecutionServer : IEntity
{
public int Id { get; set; }
public bool? IsActive { get; set; }
}
Here is my configuration file. I am trying to map my boolean property in my class to the integer field in the database.
class CommandExecutionServerConfiguration : EntityTypeConfiguration<CommandExecutionServer>
{
internal CommandExecutionServerConfiguration()
{
this.ToTable("tblCommandExecutionServers");
this.Property(x => x.IsActive).HasColumnName("Active").HasColumnType("bit");
}
}
This is not working well. The error that I am getting is:
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'
I tried adding .HasColumnType("bit")
and thought that it might take of my problem. How do I do this? Ideally I would like 0 to be false, 1 to true, null to be null, and any other number to false.
UPDATE
If I change the above to:
this.Property(x => x.IsActive).HasColumnName("Active").HasColumnType("int");
...then I get the following error:
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'.
I tried the following because I do not know if Entity Framework can handle the conversion for me.
I removed this line:
this.Property(x => x.IsActive).HasColumnName("Active").HasColumnType("int");
I then added a property to my CommandExecutionServer class
:
public class CommandExecutionServer : IEntity
{
public int Id { get; set; }
public int? Active { get; set; }
public bool IsActive
{
get
{
return (Active == 1) ? true : false;
}
}
}
There might be a better way but this works for me for now. If any one can better this then please go ahead :)
这篇关于实体框架code一流的布尔和列的整数之间的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!