1

I have a model that contains 3 properties:

public class SomeModel
{
    [Key]
    public int SomeInt { get; set; }

    public string SomeString { get; set; }

    public TimeSpan Time { get; set; }
}

If I try to add such a model to my SQLite Database an exception is thrown.

        using (var db = new MyDataContext())
        {
            var item = new SomeModel { SomeInt = 123, SomeString = "a string" , Time = new TimeSpan(1,2,3)};
            db.SomeModels.Add(item);
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                Console.ReadLine();
                throw;
            }
        }

The Exception:

System.NotSupportedException: "There is no store type corresponding to the EDM type 'Edm.Time' of primitive type 'Time'."

Is there any way to add this model to the database?

TomTomB
  • 419
  • 1
  • 4
  • 13

1 Answers1

5

I suggest to use an integer instead of TimeSpan field, example:

public Int64 TimeSpanTicks{ get; set; }     

[NotMapped]
public TimeSpan Time 
{
    get { return TimeSpan.FromTicks(TimeSpanTicks); }
    set { TimeSpanTicks= value.Ticks; }
}
Ali Bahrami
  • 5,935
  • 3
  • 34
  • 53