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?