Search code examples
c#.netentity-frameworksql-server-cetimespan

EF, How to save/retrieve TimeSpan as other data type?


SQLCe doesn't support TimeSpan, so I need to save this in some other way. Is it possible to define somewhere a default conversion, like in the DbContext, or would I need to handle this manually in the repositories? I don't like to change my entity classes just because of this.

Btw, I only save TimeSpan of < 24h.

Example would be great if there are some neat tricks.


Solution

  • Well, it seems this is basically NOT POSSIBLE.

    A TimeSpan property must be marked NotMapped, or by fluent Ignore (then saved as per Steve's answer). At least that's my understanding now.

    EF does not support type mapping or type conversion.. https://stackoverflow.com/a/12053328

    Btw, NHibernate maps TimeSpan to integer by default, when using SqLite. No conversions or tricks needed there. Not sure about other databases.