vendredi 9 décembre 2016

Search all entities with specific property and their entries

I'm using EF Code First but my models have NO relationships (PKs - FKs). So I'm trying to find a way to workaround it by using EF6 Reflections in order to avoid an entry deletion that would have relationships (same property name).

  • Lookup over all my context entities in which has any specific property (FK);
  • For every entity found, check if this entity has any entry;

  • If its true, instead of deleting my entry, set a property "Canceled" as true;

  • If its false, keep entity state deleted and save my context changes;

    var deletedProperty = myEntity.OriginalValues.PropertyNames.Where(p=> myEntity.Property(p).Name == "Deleted").FirstOrDefault();
    if (deletedProperty != null)
    {
         myEntity.State = EntityState.Modified;
         myEntity.CurrentValues[deletedProperty] = true;
    }
    
    




Aucun commentaire:

Enregistrer un commentaire