简体   繁体   English

添加和创建ODD行为

[英]Add & Create ODD behaviour

I am new to Entity Framework ORM and am confused with the below. 我是Entity Framework ORM的新手,并对以下内容感到困惑。 I am confused that my count in MessageBox both after AddObject and CreateObject is same as before adding??? 我很困惑,我在AddObject和CreateObject之后在MessageBox中的计数都与添加之前相同??? Is this some expected behavior with any ORM? 这是任何ORM的某些预期行为吗? Thanks a bunch in advance. 预先感谢一堆。

// Sample Code

MessageBox.Show("Count before assignment " + _ctxEntitiesHR.GROUPS.Count());

// Try Add
GROUPS newGroup = new GROUPS();
newGroup.GRP_ID = 187;
newGroup.GRP_NAME = "group with ID 187";

_ctxEntitiesHR.GROUPS.AddObject(newGroup);

// Try Create 
var newGrp = _ctxEntitiesHR.GROUPS.CreateObject();
newGrp.GRP_ID = 189;
newGrp.GRP_NAME = "group with ID 189";
_ctxEntitiesHR.GROUPS.Attach(newGrp);

MessageBox.Show("Count after assignment " + _ctxEntitiesHR.GROUPS.Count());

Try calling context.SaveChanges() before requesting the count. 在请求计数之前,请尝试调用context.SaveChanges()

I suspect the Count() will always run a query on the database, while your newly added item hasn't been saved yet. 我怀疑Count()将始终在数据库上运行查询,而您尚未添加新添加的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM