验证对象是否已在实体框架上下文中
当我尝试验证对象是否已位于实体框架上下文内时,我在使用 EF4 时遇到问题。
我下面有这段代码
var entityName = Entity4Test + Guid.NewGuid();
using( var ctx = new EnviroDataContext() )
{
var etc = new Entity
{
Name = entityName
};
ctx.Entity.AddObject( etc );
var q = from p in ctx.Entity
where p.Name == entityName
select p;
// Why 'q.ToList().Count == 0'?
ctx.SaveChanges();
}
我的问题是,为什么插入后我的搜索结果为空?
我知道数据在“SaveChanges”之后保留,但是如果我需要“查询”我的实体内存数据怎么办?
扩展问题
我有一个业务规则,通过添加 1 个项目 A,触发其他实体 B 的插入。 问题是,我有验证规则,插入 B,t A 必须已经存在。
由于所有这些操作都是在“SaveChanges”之前进行的,因此我收到一条错误消息,指出 EntityA 不存在。
其他情况,我有一个在表中唯一的名称字段。 如果我尝试运行 AddEntityName("bla") 两次,然后运行“SaveChanges”,我会从 DB [唯一约束] 中得到异常,即使在通过了插入验证之后,也保证了名称是唯一的。
有人有什么想法吗?
I'm getting an issue while using EF4, when I'm trying to verify if object is already inside Entity Framework context.
I have this code below
var entityName = Entity4Test + Guid.NewGuid();
using( var ctx = new EnviroDataContext() )
{
var etc = new Entity
{
Name = entityName
};
ctx.Entity.AddObject( etc );
var q = from p in ctx.Entity
where p.Name == entityName
select p;
// Why 'q.ToList().Count == 0'?
ctx.SaveChanges();
}
My question is, why my search after insertion, came out empty?
I know that the data is persisted after 'SaveChanges', but what if I need to 'query' my entity memory data.
Extending the question
I have a business rule that by adding 1 item A, triggers the insertion of others entities B.
The issue is, I have validation rule that on insertion of B,t A must already exists.
Because all of these actions are made before 'SaveChanges', I get an error that EntityA doesn't exists.
Other case, I have a Name field that is unique on a table.
If I try to run AddEntityName("bla") twice and then 'SaveChanges', I get an exception from DB [Unique constraints], even after passing my validation for insertion, that guaranties that a name is unique.
Anyone have any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您执行
.AddObject
时,它会将其添加到 EF 的内部“图表”(内存),并处于“已添加”的挂起状态。只有执行
ctx.SaveChanges()
后,更改才会保存到底层存储中。您正在编写的查询是针对数据库的,并且更改尚未保留。
因此,如果您在执行
ctx.SaveChanges()
之后执行查询,则计数将符合预期。附带说明一下,如果您想查看某个实体是否已在图表中(例如,在“附加”之前),请阅读 ObjectStateManager.TryGetObjectStateEntry。
When you do
.AddObject
, it adds it to EF's internal "graph" (memory) in a pending state of "Added".Only once you do
ctx.SaveChanges()
will the changes be persisted to the underlying store.The query you are writing is against the database, and the change hasn't been persisted yet.
So if you execute your query after you do
ctx.SaveChanges()
, the count will be as expected.On a side note, if you want to see if an entity is already in the graph (e.g before you "Attach"), read up on ObjectStateManager.TryGetObjectStateEntry.
我没有使用过 EF4,但使用过以前的版本,所以我不知道他们对 EF4 是否有不同的期望。看起来您正在尝试在实际提交之前在数据库中搜索资产。您应该先调用 SaveChanges 然后再搜索它。
I haven't used EF4 but have used the previous version so I don't know if their is a different expectation in EF4. It looks like you are attempting to search for the asset in the DB before actually committing it. You should call SaveChanges first then search for it.