0

My Problem is the following. I have a DataAccessLayer Project with a DalClass. In this Dal class I have many Methods for operations with Entity Framework. For example some pseudo-code to show how we do it.

class DalClass
{    
    public void SetEntityObject(EntityObject entityObject)
    {
        using (var context = new Entities())
        {
            context.EntityObjectSet.Attach(entityObject);
            context.ChangeTracker.DetectChanges();
            foreach (var entity in context.ChangeTracker.Entries())
            {
                entity.State = entityObject.EntityState;
            }

            context.SaveChanges();
        }
    }

    public EntityObject GetEntitObject(Guid id)
    {
        EntityObject result;
        using (var context = new Entities())
        {
            result = context.EntityObjectSet.Where(x => x.Id == Id);  
        }

        return result;
    }
}

Each time we do operations with EF we create a new instance context and do some work. My problem is that I have a method in my DalClass to get a List of EntityObjects Like this :

    public List<EntityObject> GetEntityObjectsByIds(List<Guid> idList)
    {
        using (var context = new Entities())
        {
            var query = from entityObject in context.EntityObjectSet
                        where idList.Contains(entityObject.Id)
                        select entityObject;

            return query.ToList();
        }
    }

After I return the List to my Business Layer class. I do some changes to each EntityObject, calculations, change some Property values and so on. After that I call a method from my BusinessLayer class to save the new edited List with this method:

    public void UpdateEntityObjects(List<EntityObject> newEntityObjectList)
    {
        using (var context = new Entities())
        {
            var idList = entityObjectList.Select(x => x.Id).ToList();

            var query = from entityObject in context.EntityObjectSet
                        where idList.Contains(entityObject.Id)
                        select entityObject;

            var entityObjectList = query.ToList();
            entityObjectList = newEntityObjectList;

            context.ChangeTracker.DetectChanges();
            foreach (var entity in context.ChangeTracker.Entries())
            {
                entity.State = EntityState.Modified;
            }
            context.SaveChanges();
        }
    }
  1. Problem I can not save newEntityObjectList that I Edited in my BusinessLayer. Obviously this is not working.

    entityObjectList = newEntityObjectList;

So how is the best way to save the newEntityObjectList ?

  1. Problem I have to fier this query twice

                var query = from entityObject in context.EntityObjectSet
                        where idList.Contains(entityObject.Id)
                        select entityObject;
    

Solution to 2. Problem could be if I use one Context instance for GetEntityObjectsByIds() and UpdateEntityObjects(). But in all our Dal Classes we avoided that. Even if I use one instance of Context class, this does not solve Problem 1. So how can I save newEntityObjectList ?

1 Answer 1

1

Well, my DAL class would most definetely be something like this:

class EntityObjectDAL 
{
    private ObjectContext/DbContext context = new ObjectContext/DbContext();

    public List<EntityObject> GetObjects() { return context.EntityObjects.ToList(); }
    // Tracked by context. Just change it in BL

    public void SaveObjects() { context.SaveChanges(); }
    // Just call SaveChanges(), no need for anything special...

    public void InsertObject(EntityObject eo) { context.EntityObjects.Add(eo); }

    public void UpdateObject(EntityObject eo) { context.Entry(eo).State = EntityState.Modified; }
} 

Keep it simple...

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for Respond Milos. As I said in the Quastion if I use one instance of Context it would work, but i would like to avoid that approach. Is there a way to do this with 2 Instances of Context for this example ?
Well, what are newEntityObjectList? Are those new entities or are those loaded from another context? If those are new, how do you create them: context.EntityObjects.Create() or just new EntityObject()? I suppose you always need to detach them from previous context (hence just more work in case of 2 contexts), and then attach them to new one by calling Add() or Attach() methods. And change entityState, ofcourse. That's how I would try, anyway.
newEntityObjectList is the List of Objects that I get from the first method with first method List<EntityObject> GetEntityObjectsByIds(List<Guid> idList) that uses 1 Context, the name now is just newEntityObjectList because I have done some Editing on the Objects and their Porperties
well, normally, you would need to detach entities from context, but your first context is destroyed, so i think that's not necessary. Did you try just iterating over entities in the list, attaching it to a new context via context.EntityObjectSet.Attach() and changing entity state to modified like you already do. And then just call SaveChanges()? If yes, what error do you get?
Sorry for late answer Milos. I solved the Problem with using just one Context. I made my changes to my EntityObjectList and Saved it. With 2 Instances of Context it would be much Work but I dont see the benefits right now. Thanks for answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.