Sljux
Sljux

Reputation: 534

Delete data sith Breeze.js without loading it to client

I am using Breeze.js with Entity Framework WebAPI backend, and I need to delete a large set of data that is not loaded to client. I would really like to do it on the server and not load it.

Is there a "breeze way"? By that I mean a method in a BreezeController.

EDIT

I have to delete all rows from one table that belong to the user, whose date field is in future, and all their child rows.

Upvotes: 0

Views: 276

Answers (1)

Dominictus
Dominictus

Reputation: 721

public override int SaveChanges()
    {
        foreach (
            var entry in
                this.ChangeTracker.Entries()
                    .Where((e => (e.State == (EntityState) Breeze.WebApi.EntityState.Deleted))))
        {
            if (entry.Entity.GetType() == typeof(User))
            {
                var entity = entry.Entity as User;
                var childEntitiesInFuture = ChildEntities.Where(c => c.DateField > DateTime.Now);
                foreach (var child in childEntitiesInFuture){
                  var grandchildrenForDeletion = Grandchildren.Where(c => c.ChildId == child.Id);
                  foreach (var g in grandchildrenForDeletion) Grandchildren.Remove(g);
                  ChildEntities.Remove(child);
                }
             }
         }
    }

Assuming you are deleting User, one User has many ChildEntity saved in ChildEntities and each ChildEntity has many Grandchild saved in Grandchildren. A bit messy names, but that's what you get with no real names :)

This method goes into your Context class. Good luck.

Upvotes: 1

Related Questions