如何使用 Entity Framework Core 5 管理实体的历史版本

发布于 2025-01-11 07:30:10 字数 1822 浏览 0 评论 0原文

我得到了一个如下所示的模型,其中有一些与此相关的相关表,例如地址、元信息等:

public class Contact
{
    [Key]
    public int Id { get; set; }
    
    [MaxLength(15)]
    [Column(Order = 1)]
    public string Title { get; set; }

    [Required, MaxLength(100)]
    public string FirstName { get; set; } 

    [MaxLength(100)]
    public string LastName { get; set; }

    ......some more string and int properties

    public virtual ICollection<ContactAddress> Addresses { get; set; }

    public virtual ContactMetaInfo MetaInfo { get; set; } 
}
public class ContactAddress
{
    [Key]
    public int Id { get; set; }

    [ForeignKey(nameof(Contact))]
    public int ContactId { get; set; }

    public virtual  Contact Contact { get; set; } 

    [MaxLength(150)]
    public string Line1 { get; set; }

    [MaxLength(150)]
    public string Line2 { get; set; }

    ......some more string and int properties and some foreign keys 

    [ForeignKey(nameof(Continent))]
    public int? ContinentId { get; set; }

    public virtual  Continent Continent { get; set; }

    [ForeignKey(nameof(Country))]
    public int? CountryId { get; set; }

    public virtual  Country Country { get; set; }

    [ForeignKey(nameof(County))]
    public int? CountyId { get; set; }

    public virtual  County County { get; set; } 
}

现在我正在处理一个需求。我必须创建这些实体的历史版本

我正在考虑将新类创建为实体,如下所示:

  • HistoryContact
  • HistoryContactAddress
  • HistoryContactMetaInfo

并将所有属性从 Contact 复制到 HistoryContact 及后续表。几乎有 7 个实体需要复制,所以...

将来,如果将新属性添加到这些实体中的任何一个,我必须在 2 个实体中手动处理它们[分别是主实体和历史实体]。

有没有更好的方法来处理这些场景?

目前我使用的是 EF Core 5,但也可以使用 EF Core 6。

I got a model like below which is having some related tables associated with this like Addresses, MetaInfo, etc :

public class Contact
{
    [Key]
    public int Id { get; set; }
    
    [MaxLength(15)]
    [Column(Order = 1)]
    public string Title { get; set; }

    [Required, MaxLength(100)]
    public string FirstName { get; set; } 

    [MaxLength(100)]
    public string LastName { get; set; }

    ......some more string and int properties

    public virtual ICollection<ContactAddress> Addresses { get; set; }

    public virtual ContactMetaInfo MetaInfo { get; set; } 
}
public class ContactAddress
{
    [Key]
    public int Id { get; set; }

    [ForeignKey(nameof(Contact))]
    public int ContactId { get; set; }

    public virtual  Contact Contact { get; set; } 

    [MaxLength(150)]
    public string Line1 { get; set; }

    [MaxLength(150)]
    public string Line2 { get; set; }

    ......some more string and int properties and some foreign keys 

    [ForeignKey(nameof(Continent))]
    public int? ContinentId { get; set; }

    public virtual  Continent Continent { get; set; }

    [ForeignKey(nameof(Country))]
    public int? CountryId { get; set; }

    public virtual  Country Country { get; set; }

    [ForeignKey(nameof(County))]
    public int? CountyId { get; set; }

    public virtual  County County { get; set; } 
}

Now I am dealing with a requirement. I have to create a historical version of these entities.

I am thinking of making new Class as Entities like below :

  • HistoryContact
  • HistoryContactAddress
  • HistoryContactMetaInfo

And copy all properties from Contact to HistoryContact and subsequent tables. There are almost 7 Entities are there to copy so...

In future, if a new property added to any of these entities, I have to handle them manually in 2 entities [Master and History entities separately].

Is any better way to handle these scenarios?

Currently I'm using EF Core 5, but it's possible to use EF Core 6.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文