Answered by:
Custom Save/Load logic for entities

Question
-
I'm not all too experienced with the Entity Framework and it's customizability so I have a question.
Is there is a good way to do custom Save/Load logic for entities in the Entity Framework? For example, I have some values in one of my entity classes which I'd like to encrypt in a database table, so I essentially want to encrypt the value on save and decrypt it on load from the database. I might even want to concatenate (with some delimiter) two properties from a class, encrypt them and store in a column in the database. Then on load, decrypt them, and split them into their properties again.
Is there a good way to do this custom stelp in EF?
Friday, July 23, 2010 9:02 AM
Answers
-
Hi Stebet,
I'm asking a similar question over here and not getting much insight.
http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/cc1347d3-17cc-47b3-8572-98bd2081c8f3
Doing some additional searching though has lead me to belielve this is how to do what you want:
http://msdn.microsoft.com/en-us/library/cc716714.aspx
Hope this helps.
~Tony- Proposed as answer by Michael Sun [MSFT]Microsoft employee Thursday, July 29, 2010 1:22 AM
- Marked as answer by Stebet Thursday, July 29, 2010 8:55 AM
Friday, July 23, 2010 12:57 PM -
Hello Stebet,
Welcome to EF forum!
Besides, the SavingChanges event which is suggested by Tony, we can also consider override the SaveChanges method. For detail, please see http://blog.cincura.net/230662-savechanges-is-virtual-in-ef4/.
Besides, I have some detailed explanation in this thread for your references, http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/e00d3950-ae17-4261-b88e-afd106f1a8d1.
Good day!
Best Regards,
Lingzhi SunMSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Proposed as answer by Michael Sun [MSFT]Microsoft employee Thursday, July 29, 2010 1:22 AM
Monday, July 26, 2010 3:31 AM
All replies
-
Hi Stebet,
I'm asking a similar question over here and not getting much insight.
http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/cc1347d3-17cc-47b3-8572-98bd2081c8f3
Doing some additional searching though has lead me to belielve this is how to do what you want:
http://msdn.microsoft.com/en-us/library/cc716714.aspx
Hope this helps.
~Tony- Proposed as answer by Michael Sun [MSFT]Microsoft employee Thursday, July 29, 2010 1:22 AM
- Marked as answer by Stebet Thursday, July 29, 2010 8:55 AM
Friday, July 23, 2010 12:57 PM -
Hmm.. ok. So I should be able to do that by hooking into the ObjectMaterialized and SavingChanges events on the ObjectContext class?Friday, July 23, 2010 2:10 PM
-
Hi Stebet,
Yes, seems the only way. I'm curious about the ObjectMaterialized event though, specifically about being raised before collections are populated. As long as this doesn't affect you, this should be your ticket.
Best regards.
~TonyFriday, July 23, 2010 2:27 PM -
Hello Stebet,
Welcome to EF forum!
Besides, the SavingChanges event which is suggested by Tony, we can also consider override the SaveChanges method. For detail, please see http://blog.cincura.net/230662-savechanges-is-virtual-in-ef4/.
Besides, I have some detailed explanation in this thread for your references, http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/e00d3950-ae17-4261-b88e-afd106f1a8d1.
Good day!
Best Regards,
Lingzhi SunMSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Proposed as answer by Michael Sun [MSFT]Microsoft employee Thursday, July 29, 2010 1:22 AM
Monday, July 26, 2010 3:31 AM -
Hi Stebet,
I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?
If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.
Have a nice day!
Best Regards,
Lingzhi SunMSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.Thursday, July 29, 2010 1:24 AM -
Thanks Tony and Lingzhi for your replies. I did some initial testing and it seems the ObjectMaterialized and SavingChanges events will do for my scenario. If I need to do more complicated logic I might take a look at overriding the SavingChanges method.
Again, thanks for the replies:)
Thursday, July 29, 2010 8:57 AM -
You're welcome! :-)
Have a nice weekend!
Best Regards,
Lingzhi SunMSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.Friday, July 30, 2010 1:03 AM