Get EntityFramework Added entities as gridview datasource

无人久伴 提交于 2019-12-11 19:27:16

问题


In EntityFramework 4, which is the correct way to get entities from:
- the database (minus the Deleted objects),
- and in-memory Added ones
to show them in a GridView?

Using linq to entities I get the data currently in database.
Using ObjectStateManager.GetObjectStateEntries(entityState).Select(ent => ent.Entity).OfType<IEntityWithKey>(), I get the Added and/or Deleted (as desired)

With linq to entities I'm getting the Deleted objects in the query result, so they are shown in the gridvew (as well as the Modified and Unchanged), but this is not what I want, I don't want the Deleted objects.

What I need is to show in a gridview all the data currently in database except the Deleted objects, plus the in-memory Added objets. (All this before calling Savechanges).

I have a method in a BL class that returns a typed collection and sets a gridview datasource with it.
To achieve this I do:
- a linq to entities to retreive db data (lets say Collection1),
- GetObjectStateEntries(Deleted) to get the Deleted entities (Collection2),
- GetObjectStateEntries(Added) to get the new in-memory added (Collection3)

Then iterates Collection1.ToList() to remove items in Collection2, then a Union with Collection3.

It works, but I don't like it.

Is there a better/proper way to do it? Any help/suggestion please?

Thanx in advance.

Here is some code.
Method to get database entities (Deleted objetcs are included here):

public IEnumerable<ConnectorTransformationLookUpConceptData> GetConnectorTransformationLookUpConceptsView(int idConnectorTransformation)
    {
    var data = from r in Entities.ConnectorTransformationLookUpConcept
               join c in Entities.LookUpConcept on r.IdLookUpConcept equals c.IdLookUpConcept
               ....

    return ExcludeDeleted(data).Union(AddedData(idConnectorTransformation)).OrderBy(e => e.Concept);
    }

Method to Remove the Deleted objects (called in the previous return) :

private List<ConnectorTransformationLookUpConceptData> ExcludeDeleted(IEnumerable<ConnectorTransformationLookUpConceptData> collection)
    {
        List<ConnectorTransformationLookUpConceptData> l = collection.ToList();
        var deleted = GetDeletedEntities<ConnectorTransformationLookUpConcept>();

        foreach (ConnectorTransformationLookUpConcept d in deleted)
        {
            ConnectorTransformationLookUpConceptData o = l.Find(c => c.idConnTransf == d.IdConnectorTransformation && c.idLooUpConcept == d.IdLookUpConcept);
            if (o != null) l.Remove(o);
        }

        return l;
    }

Method that is finally invoked by previous "AddedData" and "GetDeletedEntities" calls, which returns the desired objects (in EntityState.Added or EntityState.Deleted)

protected IEnumerable<IEntityWithKey> GetEntities<IEntityWithKey>(EntityState entityState)
    {
        IEnumerable<IEntityWithKey> data =
        this.Entities.ObjectStateManager.
            GetObjectStateEntries(entityState).
            Select(ent => ent.Entity).
            OfType<IEntityWithKey>();
        return data;
    }

来源:https://stackoverflow.com/questions/18085921/get-entityframework-added-entities-as-gridview-datasource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!