简体   繁体   中英

Entity Framework / Database - Archiving Foreign Key Records

I've developed an application for a client with the following relationship:

A SalesOrder has a foreign key relationship to a Customer (one-to-one).

The client now wants to be able to delete Customers so that they won't be available for any future (new) SalesOrders, but obviously I would like to retain the historical records for reporting, etc. Likewise, the record may need to be update-able in the future in rare cases, so in "Edit" mode the customer would need to be there (but ALL other deleted Customers would not)

I am looking for input on a pattern to model this, or better recommendations. My thought was to have an "Archived" bit on the Customer, but my problem is that if someone loads up an old SalesOrder I would also need to load that archived record for the DropDownList that Customers populate.

Since I'm using Entity Framework and an EntityDataSource, my guess is that I might get a runtime exception on the SelectedValue bind of the DropDownList also, but I haven't verified this.

Any thoughts or recommendations on where to start?

Thank you.

I believe I have a solution. I've been racking my brain on it all day but I think it's fairly easy. In a quick prototype it appears to do what I need it to:

I added an "Archived" bit to the Customers table (actually to the BusinessEntity table they inherit from). Then for the "Edit Sales Order" page, when querying Customers, I have a where clause which includes the customer by ID.

For example (pseudocode): SELECT CustomerID, Name FROM Customers WHERE Archived = 0 OR CustomerID = 52

That pulls all the active customers and the one customer for the record that I need. This allows the customer to still be linked to the record for editing AND doesn't generate a runtime exception with the DropDownList binding.

For reporting, I assume I'll just not filter based on the Archived bit since it's all read-only anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM