简体   繁体   English

如何处理TimeSpan以使用EF6将其保存在SQLite中

[英]How to handle TimeSpan to save it in SQLite with EF6

I have a model that contains 3 properties: 我有一个包含3个属性的模型:

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. 如果我尝试将这样的模型添加到我的SQLite数据库中,则会引发异常。

        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? 有什么办法可以将此模型添加到数据库中?

I suggest to use an integer instead of TimeSpan field, example: 我建议使用integer而不是TimeSpan字段,例如:

public Int64 TimeSpanTicks{ get; set; }     

[NotMapped]
public TimeSpan Time 
{
    get { return TimeSpan.FromTicks(TimeSpanTicks); }
    set { TimeSpanTicks= value.Ticks; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM