Page 1 of 1

Db.Single<DB_CustomSetting> exception

PostPosted: Thu Jul 06, 2023 5:26 pm
by neil.interactit
Hey guys,

I just hit a minor snafu - I'm sure I'll work around it, but thought you may like to know.

Calling the ServiceStack.OrmLite
Code: Select all
Db.Single<DB_CustomSetting>()

Causes the following exception:
Code: Select all
Invalid column name 'SY_Plugin'.
Invalid column name 'DB_CustomSettingValues'.

Some investigation shows that:
Code: Select all
public class DB_CustomSetting
{
   ...
   public virtual SY_Plugin SY_Plugin { get; set; }
   public virtual ICollection<JiwaFinancials.Jiwa.JiwaApplication.DB_CustomSettingValues> DB_CustomSettingValues { get; set; }
}
are not fields in the DB_CustomSetting table.

It seems that:
Code: Select all
public class DB_CustomSetting
{
   ...
   [IgnoreDataMember]public virtual SY_Plugin SY_Plugin { get; set; }
   [IgnoreDataMember]public virtual ICollection<JiwaFinancials.Jiwa.JiwaApplication.DB_CustomSettingValues> DB_CustomSettingValues { get; set; }
}
would resolve this.

I am guessing this could be a cross cutting issue across many entities.

Cheers,
Neil.

Re: Db.Single<DB_CustomSetting> exception

PostPosted: Thu Jul 06, 2023 5:56 pm
by neil.interactit
Just BTW, as an interim workaround, in case it helps anyone else ...

My code (with the exception) was:
Code: Select all
            var dbCustomSettingRec = Db.Single<DB_CustomSetting>(x => x.SettingName == "ProviderId");
            if (dbCustomSettingRec != null)
            {
                var acIdSettingId = dbCustomSettingRec.SettingID;

As I essentially only needed as scalar return (SettingID), I have simplified (and worked around) to:
Code: Select all
            var acIdSettingId = Db.ScalarSingle<string, DB_CustomSetting>(where => where.SettingName == "ProviderId", select => select.SettingID);
            if (acIdSettingId != null)
            {

By adding this little extension:
Code: Select all
    public static class JiwaExtensions
    {
        public static TResult ScalarSingle<TResult, TEntity>(this IDbConnection db, Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> selectExpression)
        {
            return db.Scalar<TResult>(db.From<TEntity>().Where(whereExpression).Take(1).Select(selectExpression));
        }
    }


Cheers.

Re: Db.Single<DB_CustomSetting> exception

PostPosted: Thu Jul 06, 2023 7:09 pm
by SBarnes
Hi Neil,

The code you are referring to is actually generated by a set of T4 templates, the actual annotation that is needed is the references annotation that would make orm lite realize its a separate table and read the data from there.

If you are using this inside a ServiceStack service one other option you could use is to actually execute and auto query and specify the fields on the request / query version of DB_CustomSetting (DB_CustomSettingQuery), that way it won't confuse the reference properties as actual fields in the table and only select the columns you want.

Re: Db.Single<DB_CustomSetting> exception  Topic is solved

PostPosted: Fri Jul 07, 2023 1:06 pm
by Mike.Sheen
I don't see this error.

I'm using 7.2.1 SR15, but I don't think any SR in this case is relevant.

Attached is a plugin which adds a route to the API of TestDBSingle - which performs the following:

Code: Select all
public DB_CustomSetting Get(TestDBServicesGetRequest request)
{
   return Db.Single<DB_CustomSetting>(x => x.SettingName == "TestSetting");
}


When invoked, it returns the expected result:
TestDBSingle.png


Any chance you can try the attached plugin yourself, to see if you get the error? If you don't then what are you doing different?

Re: Db.Single<DB_CustomSetting> exception

PostPosted: Fri Jul 07, 2023 2:41 pm
by neil.interactit
Thanks Scott. I have used a similar approach with Scalar/From extension. Pretty sure the idea originated from some VB code of yours doing something similar that I stumbled on ages ago!

Thanks Mike. Not sure what is going on as your test seems to work here ...
image.png


Actually, it would be because Jiwa is obviously more scared of you than me.

Turns out I'm happy with the ScalarSingle approach for the simplified coding. It possibly doesn't perform as well, but it is not in a high volume situation so all good.

Thanks again,
Neil

Re: Db.Single<DB_CustomSetting> exception

PostPosted: Fri Jul 07, 2023 3:13 pm
by Scott.Pearce
No worries.