title: Verify if object is already inside Entity Framework context
body: <p>I'm getting an issue while using EF4, when I'm trying to verify if object is already inside Entity Framework context.</p>
  
  <p>I have this code below</p>
  
  <pre><code>        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();
              }
  </code></pre>
  
  <p>My question is, why my search after insertion, came out empty?</p>
  
  <p>I know that the data is persisted after 'SaveChanges', but what if I need to 'query' my entity memory data.</p>
  
  <p><strong>Extending the question</strong></p>
  
  <p>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.</p>
  
  <p>Because all of these actions are made before 'SaveChanges', I get an error that EntityA doesn't exists.</p>
  
  <p><strong>Other case</strong>, 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.</p>
  
  <p>Anyone have any idea?</p>