0

我有我使用一週中的旗天爲真/假值的原子類。序列化類的實體數據庫存儲

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來添加它?任何提示或提示如何更好地做到這一點將不勝感激。

回答

0

這是我想出了......解決方案我創建了一個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(對我來說),並將其存儲在任何物體映射字符串屬性代表他們。