为实体数据库存储序列化 Class

Serializing Class for Entity Database Storage

我有一个 atomic class 我用来将星期几标记为 true/false 值。

public class DaysOfWeek
{
    public bool Sunday { get; set; }
    public bool Monday { get; set; }
    public bool Tuesday { get; set; }
    public bool Wednesday { get; set; }
    public bool Thursday { get; set; }
    public bool Friday { get; set; }
    public bool Saturday { get; set; }

    public bool this[string day]
    {
        get
        {
            return (bool)GetType().GetProperty(day).GetValue(this, null);
        }
        set
        {
            GetType().GetProperty(day).SetValue(this, value);
        }
    }
}

我想使用实体将其存储为单个列。我有一个看起来像这样的 POCO:

public class SSRS_Subscription
{
    [Key]
    public Guid id { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public Recurrence RecurrencePattern { get; set; }
    public DateTime StartTime { get; set; }
    public int? MinuteInterval { get; set; }
    public int? DaysInterval { get; set; }
    public int? WeeksInterval { get; set; }
    [NotMapped]
    public DaysOfWeek DaysOfWeek
    {
        get
        {
            return SerializeHelper.DeserializeJson<DaysOfWeek>(internalDaysOfWeek);
        }
        set
        {
            internalDaysOfWeek = SerializeHelper.SerializeJson(value);
        }
    }

    [EditorBrowsable(EditorBrowsableState.Never)]
    [Column("DaysOfWeek")]
    public string internalDaysOfWeek { get; set; }

    public SSRS_Subscription()
    {
        DaysOfWeek = new DaysOfWeek();
    }
}

这里的问题是,当我访问 DaysOfWeek 属性 时,我无法设置值。

var testSub = new SSRS_Subscription();
testSub.DaysOfWeek.Friday = true;
// testSub.DaysOfWeek.Friday stays false (the default value)

// However doing this sets the correct value...
var tmpDaysOfWeek = testSub.DaysOfWeek;
tmpDaysOfWeek.Friday = true;
testSub.DaysOfWeek = tmpDaysOfWeek;

我相信我需要的是一个 ObservableCollection 事件,但在搜索示例后,我不确定如何实现它。我是否修改我的实体 POCO SSRS_Subscription 以添加它?任何关于如何更好地做到这一点的提示或技巧将不胜感激。

这是我想出的解决方案...我创建了一个 ISerialize 接口然后让我的 POCO 实现它...

public interface ISerialize
{
    void Serialize();
}

public class SSRS_Subscription : ISerialize
{
    [Key]
    public Guid id { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public Recurrence RecurrencePattern { get; set; }
    public int? MinuteInterval { get; set; }
    public int? DaysInterval { get; set; }
    public int? WeeksInterval { get; set; }
    [NotMapped]
    public DaysOfWeek DaysOfWeek { get; set; }

    [Column("DaysOfWeek")]
    private string internalDaysOfWeek { get; set; }

    [NotMapped]
    public MonthsOfYear MonthsOfYear { get; set; }


    [Column("MonthsOfYear")]
    private string internalMonthsOfYear { get; set; }

    public Catalog_Reports_Subscription()
    {
        DaysOfWeek = new DaysOfWeek();
        MonthsOfYear = new MonthsOfYear();
    }

    public WhichWeek? MonthWhichWeek { get; set; }  

    public string Days { get; set; }

    public void Serialize()
    {
        internalDaysOfWeek = SerializeHelper.SerializeJson(DaysOfWeek);
        internalMonthsOfYear = SerializeHelper.SerializeJson(MonthsOfYear);
    }

    public class Configuration : EntityTypeConfiguration<SSRS_Subscription>
    {
        public Configuration()
        {
            Property(s => s.internalDaysOfWeek).HasColumnName("DaysOfWeek");
            Property(s => s.internalMonthsOfYear).HasColumnName("MonthsOfYear");
        }
    }
}

然后我对我的 DbContext 进行了以下更改:

   public ReportingDbContext() : base("ReportingDbContext")
    {
        var objectContext = ((IObjectContextAdapter)this).ObjectContext;
        objectContext.SavingChanges += new EventHandler(OnSavingChanges);
    }

    public void OnSavingChanges(object sender, EventArgs e)
    {
        foreach (ObjectStateEntry entry in
            ((ObjectContext)sender).ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified))
        {
            if (!entry.IsRelationship && (entry.Entity is ISerialize))
            {
                (entry.Entity as ISerialize).Serialize();
            }
        }
    }

每当在我的上下文中调用 SaveChanges 时,SavingChanges 都会先运行,查找任何实现 ISerialize 的对象并调用 Serialize 函数,该函数采用非实体映射属性并将它们序列化为 JSON(在我的例子中) 并将它们存储在代表它们的映射字符串属性中。