简体   繁体   English

实体框架/数据库 - 存档外键记录

[英]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). SalesOrder与客户具有外键关系(一对一)。

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) 客户现在希望能够删除客户,以便它们不会用于任何未来(新)SalesOrders,但显然我想保留报告的历史记录等。同样,记录可能需要更新 - 在极少数情况下将来可用,因此在“编辑”模式下,客户需要在那里(但所有其他已删除的客户都不会)

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. 我的想法是在客户上有一个“存档”位,但我的问题是,如果有人加载旧的SalesOrder,我还需要加载客户填充的DropDownList的存档记录。

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. 由于我正在使用Entity Framework和EntityDataSource,我的猜测是我也可能在DropDownList的SelectedValue绑定上获得运行时异常,但我还没有验证这一点。

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). 我在Customers表中添加了一个“Archived”位(实际上是它们继承的BusinessEntity表)。 Then for the "Edit Sales Order" page, when querying Customers, I have a where clause which includes the customer by ID. 然后,对于“编辑销售订单”页面,在查询客户时,我有一个where子句,其中包含按ID分类的客户。

For example (pseudocode): SELECT CustomerID, Name FROM Customers WHERE Archived = 0 OR CustomerID = 52 例如(伪代码): 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. 这允许客户仍然链接到记录以进行编辑,并且不会使用DropDownList绑定生成运行时异常。

For reporting, I assume I'll just not filter based on the Archived bit since it's all read-only anyway. 对于报告,我假设我不会根据Archived位进行过滤,因为无论如何它都是只读的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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