Code Monkey home page Code Monkey logo

Comments (8)

steveschmitt avatar steveschmitt commented on September 15, 2024

I think the reason is that the entityInfo.OriginalValuesMap cannot be trusted to provide all the fields that might be changed. For example, entity changes could happen in a BeforeSaveEntities handler that might not update the OriginalValuesMap.

I wouldn't want to change this default behavior, because it might break some existing Breeze applications, but it might make sense to make it configurable via something in BreezeConfig.

from breeze.server.net.

christianacca avatar christianacca commented on September 15, 2024

That makes sense. Would you be willing to accept a PR for this change?

from breeze.server.net.

steveschmitt avatar steveschmitt commented on September 15, 2024

Yes please! A PR would be great.

from breeze.server.net.

steveschmitt avatar steveschmitt commented on September 15, 2024

@christianacca Have you made any progress on this?

from breeze.server.net.

christianacca avatar christianacca commented on September 15, 2024

Nope! started this weekend building out some test cases around owned entities. Realistically, I would be surprised if I'm able to get a working PR in the next 4 weeks.

from breeze.server.net.

iulianb avatar iulianb commented on September 15, 2024

entity changes could happen in a BeforeSaveEntities handler

We have this scenario and so far I haven't been able to come up with a workaround.

Some of our entities are mapped to database views and they can be edited on the client. When the changes reach the server we have some bits of infrastructure that "convert" the view entities to their table mapped counterparts. Setting the state to unchanged in HandleModified discards the updates done through the converters and there's no step between that and SaveChangesCore that would allow us to reapply the current values.

I'm thinking that "accepting" changes (by manipulating OriginalValues) on the table entity properties and leaving only the entity as modified in the BeforeSaveEntities step would be an acceptable workaround but haven't got to it yet.

LE: seems to be working

var entry = _context.ChangeTracker.Entries().FirstOrDefault(entry => entry.Entity == convertedEntity);

foreach (var property in _context.Model.FindEntityType(convertedEntity.GetType()).GetProperties())
{
    var current = entry.CurrentValues[property];
    var original = entry.OriginalValues[property];

    if (current != original)
    {
        entry.OriginalValues.SetValues(new Dictionary<string, object>()
        {
            { property.Name, current },
        });
    }
}

from breeze.server.net.

steveschmitt avatar steveschmitt commented on September 15, 2024

@iulianb I've also faced a case where I had to update "view" entities on the client. Ultimately I handled it on the client: when it was time to save, I queried the appropriate table entities corresponding to the changed view entities, updated them, and saved them. This is easier than removing and adding entities in BeforeSaveEntities, but it does have a performance cost because the table entities are being brought to the client.

To handle it in BeforeSaveEntities would require similar logic. You would need to query the table entities, add them to the SaveMap, update their properties from the corresponding view entities, and remove the view entities from the SaveMap.

It reminds me a bit of this StackOverflow answer, but of course this is a different problem.

from breeze.server.net.

iulianb avatar iulianb commented on September 15, 2024

@steveschmitt

To handle it in BeforeSaveEntities would require similar logic. You would need to query the table entities, add them to the SaveMap, update their properties from the corresponding view entities, and remove the view entities from the SaveMap.

That is exactly what we're doing in the converters, which are basically command handlers executed before the handlers for create/update/delete. We've opted for this approach because in some scenarios the conversion is not 1 to 1 (eg. one view entity needs to be saved to multiple tables) and we wanted to provide developers with some degree of control over how the conversion happens. After conversion the entities go through the create/update/delete flow as there might be other implementation details, depending on the entity.

As stated in the previous reply, the issue is on updated view entities. Their table counterparts are automatically tracked by EF when queried and any property update on them ends up in the state tracker. When the entity state is set to Unchanged (happens in HandleModified) the change tracker discards these updates and returns the entity to its original state. UpdateOriginalValues does set the original values and updates the state but their current value is lost. The bit of code I posted earlier attempts to set the updated values as original in the state tracker. I'm not 100% sure that it's the correct approach but saving an entity whose tracking information has been altered in this manner seems to work correctly (in the sense that the updated value ends up in the database).

from breeze.server.net.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.