我想使用 Telerik 的 PersistenceFramework 保存我的 wpf 应用程序的“查看”状态。
我可以即时保存和加载流,like in the docs examples ,但我需要将 status
流对象存储在数据库中。
我目前正在尝试这些功能,但没有成功:
public static void SaveGridStatus(System.IO.Stream stream)
{
using (MyEntities context = new MyEntities())
{
MemoryStream ms = new MemoryStream();
stream.CopyTo(ms);
TestViewStatus t = new TestViewStatus { GridStatus = ms.ToArray() };
context.TestViewStatuses.Add(t);
context.SaveChanges();
}
}
public static Stream LoadGridStatus()
{
using (MyEntities context = new MyEntities())
{
TestViewStatus t = (from d in context.TestViewStatuses
select d).FirstOrDefault();
return t.GridStatus;
}
}
其中 TestViewStatus.GridStatus 是一个 VARBINARY(MAX)
有些东西保存在数据库中,但是当我尝试使用持久性框架将它加载到我的控件中时,没有任何反应
编辑:
根据要求将流加载到控件中的代码
private void SaveGridLayout_Click(object sender, System.Windows.RoutedEventArgs e)
{
PersistenceManager manager = new PersistenceManager();
this.stream = manager.Save(this.BrowseProjectGrid);
SaveBrowseGridStatus(this.stream);
this.EnsureLoadState();
}
private void LoadGridLayout_Click(object sender, System.Windows.RoutedEventArgs e)
{
this.stream.Position = 0L;
PersistenceManager manager = new PersistenceManager();
manager.Load(this.BrowseProjectGrid, LoadBrowseGridStatus());
this.EnsureLoadState();
}
(我想指出,如果我使用本地 Stream
对象来保存和检索 View 状态,它可以正常工作)
最佳答案
我猜,那个问题在
public static Stream LoadGridStatus()
{
using (MyEntities context = new MyEntities())
{
TestViewStatus t = (from d in context.TestViewStatuses
select d).FirstOrDefault();
return t.GridStatus;
}
}
替换为
public static Stream LoadGridStatus()
{
using (MyEntities context = new MyEntities())
{
TestViewStatus t = (from d in context.TestViewStatuses
select d).FirstOrDefault();
return new MemoryStream(t.GridStatus);
}
}
变化是 varbinary(max) 从 DB 加载并放入 Stream。
关于c# - 如何使用 Entity Framework 保存和加载 System.IO.Stream 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32442575/