Jigar Patel
Jigar Patel

Reputation: 4615

Entity Framework Retrieve navigational properties

I am using EF 5 and I have lazy loading enabled. When I retrieve an Entity from database it works perfectly.

Here is my problem. I have a generic repository to perform database operation.

    public int Update(T t) //Update method implemented at repository layer
    {
        dbSet.Attach(t);
        context.Entry(t).State = EntityState.Modified;
        return context.SaveChanges();
    }

    public T Update(T t, int id) //This Method calls the above method to
    {
        if (Update(t) > 0)
        {
            //Now entity is updated so retrieve the entity from the database.
            return Get(id); //This line of code doesn't return entity with reference. It does return the updated entity.
        }
        return null;
    }

Now when I query for the Entity using the primary key to get an updated entity it gives me updated entity, however without any reference properties. I can not user Lazy loading here because it throws an exception.

After updating enttity, I noticed dbSet.Local has the updated entity. So I tried to clear to before I retrieve updated Entity but no luck. I also tried reloading Entity via context but it doesn't reload navigational properties. I cannot use Reference property as I an using generic repository. The only way I can accomplish is to dispose and create new instance of context and dbset.

I want to return updated entity with relational properties filled. Does anyone have a good solution.

Upvotes: 2

Views: 568

Answers (2)

user1914530
user1914530

Reputation:

I have lazy loading enabled

I am attaching POCO entity

I am assuming from your comments that somewhere in your application you are instantiating your entity like so new MyEntity() and as such lazy loading will not work as it is not a proxy POCO.

The easiest way to do what you're trying to do, considering you say you have lazy loading enabled, is to work with the proxy POCOs. That is use the below to instantiate an entity wherever that may be:

MyEntity entity = MyContext.MyEntities.Create();

Lazy loading should then work for you. If you don't want to do this or this does not work then the best option is to pull the existing entity (as a dynamic proxy) from the database and populate from your POCO. So in your repository update method:

Edit

I should note that it is also possible to do this without a round trip to the db. See comments.

public T Update(T poco)
{
  //get the entity from db
  T proxyPoco = context.Set<T>().Find(id);

  //alternatively just create the proxy, set the id and attach.
  //no db retrieval.
  //T proxyPoco = context.Set<T>.Create();
  //proxyPoco.Id = poco.Id;
  //context.Set<T>.Attach(proxyPoco);

  if(proxyPoco == null) 
  {
    //throw an exception or handle case where the entity is not found.
    //unecessary if using alternative above.
  }
  else 
  {
    //set the proxy poco values using your original poco
    context.Entry<T>(proxyPoco).CurrentValues.SetValues(poco);
  }
  context.SaveChanges();
  return proxyPoco;
}

Because you return the proxy POCO lazy loading should work. Other less desirable options are to:

  1. Discard context and get entity again.
  2. Use reflection to explicitly load reference and collections for that entity.

Upvotes: 1

Travis J
Travis J

Reputation: 82337

SaveChanges returns an int. You want the entity back, try this:

public T Update(T t)
{
    dbSet.Attach(t);
    context.Entry(t).State = EntityState.Modified;
    context.SaveChanges();
    return t;
}

Upvotes: 1

Related Questions