Question

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.

Était-ce utile?

La solution 3

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.

Autres conseils

I know you say you don't want to modify your entities, but if you're using code first this would be pretty simple to do by modifying your entities :)

You could define a property that isn't mapped into the database, that uses another property as its backing store. In this example, TickCount would get saved in the database, but everything else could access Span which exposes TickCount as a TimeSpan struct.

public long TickCount { get; set; }

[NotMapped]
public TimeSpan Span { 
    get { 
        return new TimeSpan(TickCount); 
    } 
    set { 
        TickCount = value.Ticks; 
    } 
}

Pretty old but in case anyone ever needs this, I have cloned and modified System.Data.SQLite in order to support TimeSpan properties (mapped to sqlite TIME columns) You can find the source at https://github.com/arielflashner/System.Data.SQLite

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top