telerikodatatelerik-open-access

Telerik OpenAccess - Mapping Varchar to Int


I'm trying to run an integer comparison (for instance greater than or less than) using OData on a value that is set to be a type of varchar in the database. Crossing out the solution of changing the database field to be a type of int, as it's not preferred in my specific case, is there a way to tell Telerik OpenAccess to convert the field to a type of integer when executing either the query or the mappings?

Thanks in advance.


Solution

  • It is similar with this one: Handling Dates with OData v4, EF6 and Web API v2.2

    Add one more proerpty:

    public partial class Title
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string StringProperty { get; set; }
    }
    
    public partial class Title
    {
        [NotMapped]
        public int IntProperty
        {
            get
            {
                return StringProperty==null?0;Int32.Parse(StringProperty);;
            }
            set
            {
                StringProperty = value.ToString();
            }
        }
    }
    

    Update the model:

    public static IEdmModel GetModel()
    {
        ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
        EntityTypeConfiguration<Title> titleType= builder.EntityType<Title>();
        titleType.Ignore(t => t.StringProperty);
        titleType.Property(t => t.IntProperty ).Name = "MyProperty";
    
        builder.EntitySet<Title>("Titles");
    
        builder.Namespace = typeof(Title).Namespace;
    
        return builder.GetEdmModel();
    }