zzzprojects / GraphDiff

GraphDiff is a library that allows the automatic update of a detached graph using Entity Framework code first.
https://entityframework-graphdiff.net/overview
MIT License
333 stars 101 forks source link

How to Ignoring one or more fields #167

Closed alishmki closed 6 years ago

alishmki commented 7 years ago

Hi. How can I ignore one or more specific columns when I use this library? Thanks

JonathanMagnan commented 7 years ago

Hello @alishmki ,

What do you mean, you want to attach entities but ignore some column? What will be the desired behavior on SaveChanges?

I believe the final answer will be a no but let check first your requirement to be sure.

Best Regards,

Jonathan

alishmki commented 7 years ago

Yes, my entity has a "InsertedDate" field that keeps date of inserting record. when I update this record, I want to ignore this column from any changes.

one of the best ways maybe this code. by it's not work when I use GraphDiff. DataContext.UpdateGraph(entity, x => x.OwnedCollection(y => y.AttachmentFiles)); DataContext.Entry(entity).Property(x => x.InsertedDate).IsModified = false;

JonathanMagnan commented 7 years ago

Hello @alishmki ,

Thank you for your code. I will try to check during the weekend if something similar already exists or can be implemented.

I will try to get back to you for next Monday.

Best Regards,

Jonathan

cryo75 commented 7 years ago

@alishmki GraphDiff sends a complete graph to be updated back to the the database. This is because it handles only disconnected graphs and therefore state management cannot be used on the property level.

If you entity as an Id property you can do:

If (entity.Id == 0) entity.InsertedDate = DateTime.Now;

So that only new entities will have their InsertedDate set.

@JonathanMagnan There is currently no support for excluding properties from being ignored in updates.

JonathanMagnan commented 7 years ago

Hello @cryo75 , thank you for this additional info.

You are right, they are currently no way to doing it.


@alishmki , I took some time this weekend and made some progress.

I can easily ignore some property, not exactly with the code you provided but this is currently possible.

One problem with the solution used is ignored property took the value from the database.

It's something that could work for you?

If we take your example, that will mean the property InsertedDate no matter if you modified it or not will now contain the original value of the database. Since you don't want to have this property in a modified state, I believe it makes some sense.

Best Regards,

Jonathan

JonathanMagnan commented 7 years ago

Hello @alishmki ,

Could you let us know if that's a solution that will work for you?

Best Regards,

Jonathan

amirhz commented 6 years ago

@alishmki see this, i think this link can solve your problem.