实体框架4:添加被锁定的实体在保存之前不会显示计数 [英] Entity Framework 4: Adding dervied entity won't show in count until save

查看:106
本文介绍了实体框架4:添加被锁定的实体在保存之前不会显示计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个派生自BaseEntity的投资实体。
如果我创建并添加如下新的投资:

  investment = new Investment(); 
investment.Name =投资01;
_context.BaseEntities.AddObject(entity);

并查询计数(保存更改前)

  _context.BaseEntities.OfType<投资GT;()计数(); 

它返回零。 _context.SaveChanges();



我在做什么错?

解决方案

你没有做错什么这就是EF的工作原理。在执行 SaveChanges 之前,在数据库中不会进行任何更改 - 它被称为工作单元。您准备内存中的所有更改,然后将它们全部保存在一个事务中。


I have an "Investment" entity that is derived from "BaseEntity". If I create and add a new "Investment" like so:

investment = new Investment();
investment.Name = "Investment 01";
_context.BaseEntities.AddObject(entity);

and query the count (before saving changes)

_context.BaseEntities.OfType<Investment>().Count();

it returns zero. The same line returns 1 after _context.SaveChanges();

What am I doing wrong?

解决方案

You are not doing anything wrong. This is how EF works. No changes are done in database until you execute SaveChanges - it is called unit of work. You prepare all changes in memory and then save them all together in one transaction.

这篇关于实体框架4:添加被锁定的实体在保存之前不会显示计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆