使用ASP.Net Core中的历史表存储历史数据



我的数据库中有一个实体与多个实体链接,我需要在基本实体和子实体中存储更改(插入、更新和删除(的历史数据。

现在,我们正在考虑采用的方法是将每个数据都保存在相应的历史实体中。基本表如下所示:

public partial class Con 
{
public Guid Id { get; set; }
public string Note { get; set; }
...
public virtual ICollection<Document> Document { get; set; }
public virtual ICollection<ConLine> ConLine { get; set; }
public virtual ICollection<Leg> Leg { get; set; }
}

历史表是这样的,我不确定如何设计历史表来保存链接表数据

public partial class ConHistory 
{
public Guid Id { get; set; }
public Guid ConId { get; set; }
public int TransactionId { get; set; }
...
public virtual ICollection<Document> Document { get; set; }
public virtual ICollection<ConLine> ConLine { get; set; }
public virtual ICollection<Leg> Leg { get; set; }
}

我该如何处理这个问题?最佳行业实践是什么?我主要关心的是,当孩子的数据被更改时,我如何登录到父历史记录表和相应的子历史记录表。

对于简单的时间序列数据,保留一个带有可修改字段副本的单独表是一种非常有效的方法。不幸的是,在您的情况下,您还需要复制每个链接表,以便维护一致的外键,例如DocumentHistoryConLineHistoryLegHistory。这是很多重复的代码。然后你必须考虑,当模式改变时,所有的历史记录会发生什么

就我个人而言,我会将这些信息作为json存储在一个文本列中。您搜索的所有字段都应该在sql中,这样您就可以对其进行索引,但其余字段可以序列化为json字符串:

public partial class ConHistory 
{
public Guid Id { get; set; }
public Guid ConId { get; set; }
public int TransactionId { get; set; }
public Guid ModifiedByUser { get; set; }
public DateTime Date { get; set; }
// Serialize the rest of the `ConHistory` fields to a json object, and store them here
public string Json { get; set; }
}

Sql还有JSON_VALUE函数,如果您确实需要从json字符串中获取值进行查询,那么在Entity Framework中有一些使用该函数的示例。

最新更新