Using Entity Framework with historical data
I'm building a windows application in .Net 4.0 to create and organize electronics projects. The main purpose of the application is to record the vendor information for the electronics components (part #, description, price, etc.) and organize (associate) them into projects (finished products). One of the requirements is to track changes for any given vendor item (mainly price) and changes at the project level to provide a point-in-time statistics at both component level and project level details for changes.
I decided to use Entity Framework 4 for my data access layer with SQL CE 3.5 for the database, given the simplicity of client deployment. The data access works great, but when attempting to create the relationships (associations) between objects, the framework doesn't appear to have any obvious way to use historical data. This is my first attempt at using the entity framework, so I figured it might just be my inexperience that's keeping me from finding the answer. Here's my basic schema:
I have 3 main tables: Project, Product, and ProjectProduct
The Project and Product tables each have an ID column and a DateAdded column which are used as the complex key. The ProjectProducts table has the IDs for each of the other two tables and maintains the many-to-many relationships between the entities. The relationship table also has a DateAdded column to track changes in the product / project assoc开发者_JAVA技巧iations.
While the Entity Framework seems to work great with maintaining associations that are direct (no date criteria) using the standard code-generated data objects, it's a bit confusing on how to get it to load the associations for a point-in-time historical data schema. Essentially I need to be able to load the data objects based on the date criteria for the point-in-time requirements (parametrized loading).
Has anyone done anything similar and can point me in the right direction?
Sorry for the long explanation, but thanks in advance for any help you can provide!
I implemented the exact same thing. It's pretty easy with EF4. You basically handle the OnSavingChanges event, and enumerate the set of changed items, storing them as you please.
The only problem is, it is VERY tricky to get inserted items (unless you are ok with not having a Primary Key of the new item, which I was not) I decided to only track updates and deletes.
This article shows you how to do it, though my implementation was a lot simpler (I didn't like storing changes in XML so I made a separate table for columns)
Implementing Audit Trail using Entity Framework - Part 1
part 2 shows how to do rollbacks, if you are interested in that.
Implementing Audit Trail using Entity Framework - Part 2
精彩评论