如何用实体框架保存和加载System.IO.Stream对象
本文关键字:System IO Stream 对象 加载 何用 实体 框架 保存 | 更新日期: 2023-09-27 18:11:30
我想使用Telerik的PersistenceFramework保存wpf应用程序的"view"状态。
我可以保存和加载流的动态,就像在文档的例子,但我需要在数据库中存储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
对象来保存和检索视图状态)
我想问题出在
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.