私はを使用して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にします。
アップデート
上記を次のように変更した場合:
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'.