我在 .Net Core 中有两个课程
班上Owner
namespace CustomStoreDatabase.Models
{
public class Owner
{
public string OwnerId { get; set; }
public DateTime MinDateTime { get; set; }
public DateTime MaxDateTime { get; set; }
public TimeSpan Interval { get; set; }//store like a double
public Ownership Ownership { get; set; } //store like a JSON String in the db
}
}
和班级Ownership
namespace CustomStoreDatabase.Models
{
public class Ownership
{
public string OwnershipId { get; set; }
public List<string> TextOutput { get; set; }
public DateTime DateTime { get; set; }
public TimeSpan MeanInterval { get; set; }//Store like long ticks, TimeSpan.FromTicks(Int64), TimeSpan.Ticks
}
}
现在,我想像 String 一样存储 Ownership 类,而 MeanInterval 像 long 一样存储在数据库中。我正在检查this和this,但我不明白如何应用它。
protected virtual void OnModelCreating (System.Data.Entity.DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Owner>().Property(x => x.Ownership).HasColumnType("text");
modelBuilder.Entity<Owner>().Property(x => x.Interval).HasColumnType("float");//store like a double
//How to do both conversion Ownership to JSON String,
// but MeanInterval (TimeSpan) needs to be converted to long (Ticks) too!
}
我知道我需要使用该HasConversion()
方法,但我不知道具体该怎么做!
我将如何实现(或使用)HasConversion(Ownership
到 JSON 字符串和MeanInterval
长)?