c# - 保存对象并立即将其取回

标签 c# visual-studio entity-framework ado.net

我想保存一个对象,然后从数据库中检索它,但不知何故,保存有效,但检索返回 null。

public static Work startWork(Work work)
{
   Context.context.Work.AddObject(work);
   Context.context.SaveChanges();
   Work result = (from r in Context.context.Work
                  where r.ReceiptID == work.ReceiptID && r.begin == work.begin
                  select r).FirstOrDefault();
   return result;
}

因此,我传递了一个已经设置了 beginReceiptID 的工作实例,以便我可以检索它,但它只是返回 null。

最佳答案

更新对象后,您可以调用ObjectContext.Refresh方法让 EntityFramework 从数据库中获取对象的最新数据。

关于c# - 保存对象并立即将其取回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19866689/

相关文章:

c# - OpenXML 从 Word 中删除项目符号或数字

c# - Windows 控制台应用程序卡住(需要按键)

entity-framework - Entity Framework 在我的 WebAPI Mvc4 解决方案中无法使用

c# - InstancePerRequest DbContext ASP.NET MVC

c# - 无法打开登录请求的数据库 "test"。登录失败。用户 'xyz\ASPNET' 登录失败

c# - WPF Datagrid 绑定(bind)和列显示

python - 使用内部命令窗口 Python 工具和 Visual Studio 2013 进行调试

c++ - 使用具有相对路径的文件的 Visual Studio 单元测试

visual-studio - MSBuild + Visual Studio : Customize default "Copy Local" for all projects in the solution

c# - 在 Entity Framework 中实现 "generic"机制来处理时态数据