Comments (8)
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.
That makes sense. Would you be willing to accept a PR for this change?
from breeze.server.net.
Yes please! A PR would be great.
from breeze.server.net.
@christianacca Have you made any progress on this?
from breeze.server.net.
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.
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.
@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.
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)
- [NotMapped] properties are returned when querying the entity HOT 1
- The binary operator GreaterThan is not defined for the types 'System.String' and 'System.String' HOT 3
- .NET 6 Support HOT 9
- inlineCount() does not work when is used alone HOT 3
- How comes copied sources for .net 6, 5, and 3 HOT 1
- Breeze.server and FIPS HOT 8
- EF 6 with MySql - error HOT 5
- Json Column support? HOT 2
- FEATURE REQUEST: Provide advanced OrderBy of compound Predicate HOT 1
- Support aggregate functions in queries HOT 2
- Generation of metadata fails when an entity has two properties with the same owned type. HOT 2
- Many-to-many EF metadata maps Dictionary to "Dictionary'2" which is not a valid TS class name HOT 1
- Cancellation Token HOT 3
- Duplicate owned entity causes MetadataBuilder to throw ArgumentException HOT 2
- Do we need .8 sln/csprojs? HOT 3
- OnActionExecutionAsync not overridable HOT 15
- enums datatype is now Int32 after migrating to .net 8 HOT 11
- "No coercion operator is defined between types" when using "in" for a nullable Int property HOT 1
- Error when using "in" on an array of Enum strings HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from breeze.server.net.