how to select an inserted entity before calling savechanges

May 29, 2014 at 10:29 PM
Edited May 30, 2014 at 12:22 PM
Hello, I'm currently using this generic repository implementation and I really like it, however, I found a little issue which I would to know if is it by design or if I might be doing something wrong, basically I have a code a little bit like this:
public class CA {
    protected IUnitOfWork UnitOfWork{get; private set;}
    public CA() {
        //instantiates the unit of work here
    public void MA() {
        var codeInserted = InsertInstance();
    public string InsertInstance() {
        var code = "A";
        UnitOfWork.Repository<EntitySetA>().Insert(new EntitySetA(){
            Code = code
        return code;
    public void ReadInstance(string code) {
        var entity = UnitOfWork.Repository<EntitySetA>().Query(x=>x.Code == code).Select();
        if ( entity != null ) {
            //never reaches this line
In other words, I have a method "InsertInstance" that creates an entity and inserts it into the repository (without calling SaveChanges), and then another method "ReadInstance" that tries to find this entity by selecting it through the code returned by "InsertInstance".
However, when "ReadInstane" queries the repository, nothing is returned, obviously if I call UnitOfWork.SaveChanges() and then try to select, the object is found, unfortunately I have a scenario which instead of passing the instance created by "InsertInstance", I need to simply pass the "codeInserted", and now I'm struggling trying to figure out what I might be doing wrong.

To sum things up, should the entity be found when I query for it even though SaveChanges didn't get called, if so, is there anything else I must do to accomplish this?

P.S.: when I debug the repository, I can find the instance created inside _dbSet.Local when .Select is called, however, for some reason it's ignored and it doesn't return at all

Thanks in advance!
Marked as answer by lelong37 on 6/3/2014 at 11:01 AM
Jun 3, 2014 at 7:01 PM
Answered your question under issues.
Marked as answer by leandrokoiti on 6/3/2014 at 12:54 PM