urfnet / URF.Core

Unit of Work & Repositories Framework - .NET Core, NET Standard, Entity Framework Core. 100% extensible & lightweight.
https://github.com/urfnet
MIT License
309 stars 62 forks source link

How to save related data from a service? #37

Closed vialeda closed 6 years ago

vialeda commented 6 years ago

Hello, is this possible to save related data without the need of creating a service for each children entities? Let's say you have this post entity:

  1. Post entity have a collection of PostTranslation
  2. Post entity have a collection of PostField
  3. Then PostField also have a collection of PostFieldTranslation

In order to save it all, I have to create a PostService, PostTranslationService, PostFieldService and a PostFieldTranslationService class. They all have in common an insert method.

Then I have to call them all from PostService:

First => this.Insert(post); Then => postTranslationService.Insert(post.PostTranslations); And then => postFieldService.Insert(post.PostFields, postFieldTranslationService);

I wonder if there is a way to tell EntityFramework with URF.Core to insert them all from this.Insert(post). The version I use from now is 1.0.0-rc1.

Thank you for any advice,

David

Crazybutch commented 6 years ago

If I understood your question, this could help you (same question i've asked some time ago): https://github.com/urfnet/URF.Core/issues/24

tonysneed commented 6 years ago

Yes I concur with @CrazyButch — you can do graph updates with URF support for my Trackable Entities framework.

tonysneed commented 6 years ago

Let me know if you need further assistance and I’ll re-open.

vialeda commented 6 years ago

Would you give me a quick sample please of how setting a trackable property on objects in the graph?

vialeda commented 6 years ago

Also, does trackable property take care of Update/Insert/Delete?

tonysneed commented 6 years ago

If you use the Trackable URF packages and derive your entities from the base Entity class, each object will have a TrackingState property which you can set (usually on the client). This enum has values from Unchanged, Added, Modified, Deleted.

Then in your API all you have to do is call ApplyChanges, followed by SaveChangesAsync.

vialeda commented 6 years ago

Work very well, thank you !! :)

Domitnator commented 5 years ago

Took me a while to figure this out, so for everyone else:

If you are working with OData (like i do) the TrackingState is not automatically added as a property of your model/entity. You have to do this by hand via the ODataConventionModelBuilder:

entitySetConfigurationShipment.EntityType.EnumProperty(x => x.TrackingState);