序列化类的实体数据库存储

问题描述:

我有我使用一周中的旗天为真/假值的原子类。序列化类的实体数据库存储

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并调用序列化功能,这需要非实体映射属性并将它们序列化到JSON(对我来说),并将其存储在任何物体映射字符串属性代表他们。