Mapping ShortInt to Boolean

HI,

I have a database ( existing application) that uses shortint SQL datatype for its boolean values.
The application does this not always in its own application so in one table a shortint is simply a shortint and some other tables a shortint is a treated as an boolean ( 0 and 1 ) .

Infering the database with Radzen I want to change that to map the shortint in some tables to a boolean.

How to ? so that I do not end with adding these model and controllers to the exclude list and changing the generated model code .
is some kind of override in a custom.cs partial possible ?

Any experience ?

Hi @mcanavar,

You can check my response in this thread: My database hase a field that is integer ,i can't get it to function with Check Box

It shows how to create a custom calculated property.

The other option is to try editing the JSON file of the data source and set the "type" of the column to "boolean".

Hi @korchev,

So adding a [NotMapped] decorated new column to the model ? Radzen designer would not recognise this column right ?

Yes, Radzen does not recognize custom columns in Angular applications (but it does in Blazor ones). The other suggestion (modifying the data source JSON file) should work in design time though.

@korchev

Modified Json and it does compile but getting erro:
System.InvalidCastException: Unable to cast object of type 'System.Int16' to type 'System.Boolean'.
at Microsoft.Data.SqlClient.SqlBuffer.get_Boolean()
at Microsoft.Data.SqlClient.SqlDataReader.GetBoolean(Int32 i)
at lambda_method(Closure , QueryContext , DbDataReader , ResultContext , Int32[] , ResultCoordinator )

Then it must be some limitation of Entity Framework that I didn't know about. I found this page which seems to explain why it doesn't work. Using a [NotMapped] property is the only way to achieve what you are after.

@korchev
Thank you , sometimes it is what it is when you have to work with existing databases where you do not have any control over.
I'll let you know if I come across a better solution for this

Update :
Solution:
1: Create partial class of tableName.cs like e.g. tableName.custom.cs under Models.Context.
Add a new bool field to the model and decorate it with [NotMapped]:

[NotMapped] 
public bool shortfield_tobool { get; set; }

2: Add a partial class to the controller of the table , tableNameController.Custom.cs.

Add the Read , Get and Update Methods to the partial class.

partial void OntableNameRead(ref IQueryable<Models.MyDataBase.tableName> items)
{
   foreach (var item in items)
    {
                 switch (item.shortfield)
                {
                    case 1:
                        item.shortfield_tobool = true;
                        break;
                    case 0:
                        item.shortfield_tobool = false;
                        break;
                    default:

                        item.shortfield_tobool = false;
                        break;
                }      
    }
}

partial void OntableNameGet(ref IQueryable<Models.MyDataBase.tableName> items)
{
   foreach (var item in items)
    {
                 switch (item.shortfield)
                {
                    case 1:
                        item.shortfield_tobool = true;
                        break;
                    case 0:
                        item.shortfield_tobool = false;
                        break;
                    default:

                        item.shortfield_tobool = false;
                        break;
                }      
    }
}

partial void OntableNameUpdated(Models.MyDataBase.tableName item)
{
                 switch (item.shortfield_tobool )
                {
                    case true:
                        item.shortfield = 1;
                        break;
                    case false:
                        item.shortfield = 0;
                        break;
                }      
}

3: Add a partial class to Startup.cs : Startup.Custom.cs to expose the new field to Odata

partial void OnConfigureOData(ODataConventionModelBuilder builder) {
          var tableN = builder.EntitySet<MyContext.Models.MyDataBase.tableName>("tableNameInDatabase");
          tableN.EntityType.Property(od => od.shortfield_tobool);
}

In Radzen this field is not available in the designer , but you can add it manually by typing the name .

Hope that this helps someone else.