In ADO.Net data services, how do I check if an object is already in context?

I have an ADO.Net data service that I use to import data. There are a number of objects that are associated with most objects. To do this, during import, I first create these objects, save them, and then use .SetLink (EntityImport, "NavigationProperty", CreatedEntity). Now the first problem I ran into was that the context did not always know about CreateEntity (this is due to the fact that each of the entities imported independently, and creating a context when creating each element - I would like to keep this functionality - t .e. I try to avoid "just use one context" as an answer).

So, before trying to call SetLink, I have .AddToCreatedEntityType (CreatedEntity). This, of course, works for the first time, but in the second pass I get the error "the context is already tracking the entity."

Is there a way to check if the context is already tracking the object (context.Contains (CreatedEntity) has not yet been implemented)? I was thinking of trying to catch a try and just avoid the error, but this seems to create a new CreateEntity every pass. It seems like I need to use LINQ to Data Services to get this CreateEntity every time, but it seems inefficient - any suggestions?

+3
source share
2 answers

I think you should look at the EntityState property of your entity.

EntityState.Detached, .

:

FlagsAttribute , .

:

public static class EntityObjectExtensions
{
    public static Boolean IsTracked(this EntityObject self)
    {
        return (self.EntityState & EntityState.Detached) != EntityState.Detached;
    }
}
+5

, , ( ), , , context.Entites.Contains(currentItem) .

, :

if (context.Entities.Where(entities => entities.Entity == currentItem).Any())
{
   this.service.UpdateObject(currentItem);                    
}
+3

Source: https://habr.com/ru/post/1698621/


All Articles