Question

Have 2 MySQL databases. One is the main database, the other is used for geolocation data. Now, for SubSonic to play nice with the generated files via Subcommander, I made it easy and just created a view that maps to the geolocation data in the other database (that way all the tables and geolocation data is technically in one database).

Now, the issue I'm running into is this: In the geolocation table, there are 2 fields (latitude, longtitude) which are both floats.

When I run your standard SubSonic statement to get the data:

return new Select()
            .From(ZipDatum.Schema)
            .Where(ZipDatum.Columns.Zipcode).IsEqualTo(zipCode)
            .ExecuteSingle<ZipDatum>();

I get this exception in my tests project: 'TestCanGetZipData' failed: System.ArgumentException : Object of type 'System.Single' cannot be converted to type 'System.Decimal'.

It looks like SubSonic likens float fields to decimal. But this exception is throwing me for a loop. Any way to get around this? FWIW, on all of the other 200+ tables we're using decimal for our fields that require such. But since this is a 3rd party database table, they're using float and it's causing issues.

Anyone run into this situation?

Was it helpful?

Solution

One solution would be to use the ExecuteTypedList method.

  1. You have to create a DTO class:

    public class ZipDatumHelperClass
    {
        public int Id {get;set;}
        public string ZipCode {get;set;}
        public single Latitude {get;set;}
        public single Longitude {get;set;}
    }
    
  2. rewrite your Query to

    List<ZipCodeHelperClass> result = new Select()
        .From(ZipDatum.Schema)
        .Where(ZipDatum.Columns.Zipcode).IsEqualTo(zipCode)
        .ExecuteTypedList<ZipDatumHelperClass>();
    

The ExecuteTypedList method is a very generic approach since it tries to match columns from a DataReader to properties of your class. It is very specific since it is

a Case Sensitive
b your properties have to match exactly the system type retured by the DataReader

Just let your test run and modify the result until you don't get any errors.

Another solution would be to modify the SubSonic source to generate system type single for the views mysql type.

https://github.com/subsonic/SubSonic-2.0/blob/master/SubSonic/DataProviders/MySqlDataProvider.cs

public override DbType GetDbType(string mySqlType) { }

get's the DbType from mysqltype (decimal is created for decimal, float, newdecimal, numeric, double, real) You have to figure out which one is wrong here and return DbType.Single instead.

https://github.com/subsonic/SubSonic-2.0/blob/master/SubSonic/Utility.cs

public static string GetSystemType(DbType dbType) { }

FYI: get's the System.Type for the specified DbType. You shouldn't modify that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top