How to map to DateTime stored as string?

19 views
Skip to first unread message

Craig Schulte

unread,
Dec 4, 2012, 11:19:46 PM12/4/12
to catnap-or...@googlegroups.com
I'm using your latest code.

How can I map from a DateTime value stored as a string in the database to a strongly typed DateTime column in the c# code?

The Date is stored in this format YYYY-MM-DD HH:MM:SS.SSS as text in the database.

Thanks.

Tim Scott

unread,
Dec 5, 2012, 12:32:34 AM12/5/12
to catnap-or...@googlegroups.com
Craig,

It can be done, but I'm sorry to say there's simple way. I'm afraid the built-in DbAdapters have type conversion kinda baked in. You would need to create your own custom IDbAdapter.  It implements:

object ConvertToDb(object value);
object ConvertFromDb(object value, Type toType);

Look here for an example of how these are built: 

Notice the typeConverter variable; it's set to a MySqlValueConverter.  Let's look at that:

Notice this element: { typeof(DateTime), new DateTimeTicksType() } which refers to this:

So you would create DateTimeStringType and a use that in a new custom ValueConverter which you then use in your new IDbAdapter which you set in configuration with Fluently.Configure.DatabaseAdapter(yourAdapter).[yadda]...

This definitely sucks. It would be much better to have the ability to set custom types, ideally per column, or at least database wide without having the redo the whole adapter stack. On the positive side, we've got things broken down nicely into small parts, so it might not be very hard to do such an enhancement.  

However, it's not something I would develop anytime soon.  Of course we love pull requests!

-- 
Tim Scott
Lunaverse Software

Craig Schulte

unread,
Dec 5, 2012, 4:26:08 PM12/5/12
to catnap-or...@googlegroups.com
Thanks Tim.  That's kind of what I was thinking.  Actually I didn't realize it would be as easy as creating a new IDbAdapter.  Sounds good enough for me.

I do agree that being able to specify a converter per column would be nice.  I understand that you will not be adding that anytime soon ;)

I think I might actually jump back to an older version of your project that a colleague used on a previous project for our company. (the one where you have to subclass Entity for all your entities).  I don't know the exact version number or time period that the code is from but I was wondering if the older code might me more stable than the newer code?

I haven't encountered any bugs in the newer code yet but I am concerned about stability.

Thanks again.
Reply all
Reply to author
Forward
0 new messages