c# - 如何使用 Entity Framework 保存和加载 System.IO.Stream 对象?

标签 c# wpf entity-framework telerik persistence

我想使用 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/

相关文章:

c# - 使用 .resx 文件本地化 Prism 4 模块

.net - Entity Framework 4.1:如何按对象ID删除

c# - 使用 Entity Framework 执行后期绑定(bind)存储过程

c# - 设置数据绑定(bind) DropDownList 的 SelectedValue

c# - 从 Azure Web 应用程序提供 Logstash。如何?

c# - 使用 ReSharper 在 Visual Studio 的自动格式化中换行和换行

wpf - 如何获取 WPF 窗口控件集合

c# - 通过 Microsoft.Web.Administration 使用用户名/密码创建 AppPool

c# - M-V-VM,Model不是漏进了View吗?

c# - 在 Entity Framework 中,如何在保存之前调用实体上的方法