将Entity Framework与历史数据结合使用

时间:2021-04-07 02:30:52

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.

我正在.Net 4.0中构建一个Windows应用程序来创建和组织电子项目。该应用程序的主要目的是记录电子组件的供应商信息(部件号,描述,价格等),并将它们组织(关联)到项目(成品)中。其中一个要求是跟踪任何给定供应商项目(主要是价格)的变化以及项目级别的变更,以便在组件级别和项目级别详细信息中提供变更的时间点统计信息。

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:

鉴于客户端部署的简单性,我决定将Entity Framework 4用于数据访问层,使用SQL CE 3.5作为数据库。数据访问很有用,但是当试图创建对象之间的关系(关联)时,框架似乎没有任何明显的方法来使用历史数据。这是我第一次尝试使用实体框架,所以我认为这可能只是我的经验不足让我无法找到答案。这是我的基本架构:

I have 3 main tables: Project, Product, and ProjectProduct

我有3个主要表:Project,Product和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 associations.

Project和Product表每个都有一个ID列和一个DateAdded列,用作复杂键。 ProjectProducts表具有其他两个表中每个表的ID,并维护实体之间的多对多关系。关系表还有一个DateAdded列,用于跟踪产品/项目关联中的更改。

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!

很抱歉有很长的解释,但提前感谢您提供的任何帮助!

1 个解决方案

#1


10  

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.

我实现了完全相同的事情。使用EF4非常简单。您基本上处理OnSavingChanges事件,并枚举更改的项目集,并根据需要存储它们。

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)

本文向您展示了如何做到这一点,虽然我的实现更简单(我不喜欢在XML中存储更改,所以我为列创建了一个单独的表)

Implementing Audit Trail using Entity Framework - Part 1

使用实体框架实现审计跟踪 - 第1部分

part 2 shows how to do rollbacks, if you are interested in that.

如果您对此感兴趣,第2部分将介绍如何进行回滚。

Implementing Audit Trail using Entity Framework - Part 2

使用实体框架实现审计跟踪 - 第2部分

#1


10  

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.

我实现了完全相同的事情。使用EF4非常简单。您基本上处理OnSavingChanges事件,并枚举更改的项目集,并根据需要存储它们。

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)

本文向您展示了如何做到这一点,虽然我的实现更简单(我不喜欢在XML中存储更改,所以我为列创建了一个单独的表)

Implementing Audit Trail using Entity Framework - Part 1

使用实体框架实现审计跟踪 - 第1部分

part 2 shows how to do rollbacks, if you are interested in that.

如果您对此感兴趣,第2部分将介绍如何进行回滚。

Implementing Audit Trail using Entity Framework - Part 2

使用实体框架实现审计跟踪 - 第2部分