5

私はを使用して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'.
4

2 に答える 2

1

Entity Frameworkが変換を処理できるかどうかわからないため、次のことを試しました。

私はこの行を削除しました:

this.Property(x => x.IsActive).HasColumnName("Active").HasColumnType("int");

次に、プロパティを追加しました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;
          }
     }
}

より良い方法があるかもしれませんが、これは今のところ私にとってはうまくいきます。誰かがこれをより良くすることができるなら、先に進んでください:)

于 2013-03-07T10:43:27.547 に答える
1
 SELECT   CONVERT(A.bitcolumn as bit) as bitout

ado.netはビットをboolに変換します。したがって、t-sqlのselectステートメントで整数をビットに変換するだけです。

于 2015-12-21T15:00:57.937 に答える