Entity framework code first convert between class boolean and column integer
G

3

6

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'.
Giesecke answered 7/3, 2013 at 9:39 Comment(3)
have you tried with HasColumnType("int") ? With the same problem, I set the column type to bit in SQL server.Diploma
See my update above. Yes I would also want to set it to bit but it's not my server so I can't go change table structures :)Giesecke
Does this answer your question? Entity Framework C# convert int to boolDelora
G
1

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 :)

Giesecke answered 7/3, 2013 at 10:43 Comment(2)
the point here is: IsActive is unknown from the sql server, so if you query using this field in a where, you'll have to get all the table lines client side. BTW, have you an Ignore(x => x.IsActive) in your context ? And if not, Does the mapping run UnExceptionnaly ?Diploma
No I don't have it as Ignore. No exception this way.Giesecke
A
1
 SELECT   CONVERT(A.bitcolumn as bit) as bitout

ado.net will convert bits to bools. So, just convert your integer to a bit in your select statement in t-sql.

Azote answered 21/12, 2015 at 15:0 Comment(0)
D
0

In Entity Framework Core you can use Value Conversions like this:

class CommandExecutionServerConfiguration : EntityTypeConfiguration<CommandExecutionServer>
{
     internal CommandExecutionServerConfiguration()
     {
          this.ToTable("tblCommandExecutionServers");
          this.Property(x => x.IsActive)
          .HasColumnName("Active")
          .HasConversion<int>();
     }
}

Similar question like this

You can learn more here

Delora answered 2/2 at 17:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.