-
Notifications
You must be signed in to change notification settings - Fork 600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wishlist: Support DateOnly
properties
#665
Comments
I'll take a look at this, see what's going on. As someone who works with a number of codebases still on .NET Framework, I'm in favor of maintaining as much compatibility as possible. Conditional compiles can be your friend. In this case, explicitly supporting In the short term, either a value converter or a custom mapper would be the way to go for you. A value converter would have to be applied to each |
@asherber , can you point me to an example of a custom-mapper (or provide a custom-mapper for the DateOnly type)? |
There are several ways of going about this. Here's one. create table date_test (Foo date not null);
insert into date_test (Foo) values ('2024-08-01'); class DateMapper : ConventionMapper
{
public override Func<object, object> GetFromDbConverter(PropertyInfo targetProperty, Type sourceType)
{
// see if there's already a converter on the class/property
var result = base.GetFromDbConverter(targetProperty, sourceType);
if (result == null
&& targetProperty.PropertyType == typeof(DateOnly)
&& sourceType == typeof(DateTime))
{
return o => DateOnly.FromDateTime((DateTime)o);
}
return result;
}
}
class DateTest
{
public DateOnly foo { get; set; }
}
using var db = new Database(Connection, new DateMapper());
var records = db.Fetch<DateTest>("select * from date_test"); |
Given the preconditions (that we need to support Netstandard 2.0+, .NET Framework 4.0 and 4.5+), I might be asking for the impossible here, but anyway... 🙂
Trying to read a
DATE NOT NULL
field in PostgreSQL, but it causes the following failure:My guess is that I need to resort to a custom converter, i.e. create a class which inherits from
ValueConverterAttribute
and override the methods there for now? 🤔 To manually handle such properties.Would be awesome to be able to drop all "old" platforms here to be able to fully utilize the goodness of
DateOnly
, which is .NET 6 and 7 only... but I realize I might be one of the privileged few in this case, not having to deal with any legacy .NET codebases. 😁 (My day job is as a Java programmer. 😂)The text was updated successfully, but these errors were encountered: