如何向多对多关系表添加列(操作时间)?

发布于 2024-12-14 05:09:08 字数 1262 浏览 0 评论 0原文

我有一个表或域实体 ChargaOperation,它映射到表 [ChargeOperation] 并且与自身有多对多的关系。它就像客户债务的抵消或分配树(父级和一级子级)。

ChargeOperation
{
  int Id,
  decimal Amount,
  IList<ChargeOperation>  DistributedDebts // a tree of distributions using many to many mapping
}

关系是在[Distributions]表的帮助下实现的,该表在代码中具有映射,并且映射如下:

 public ChargeOperationMap()
    {
        Table("ChargeOperations");
        Id(x => x.Id).GeneratedBy.Identity();
        Map(x => x.Amount).Not.Nullable().Column("Amount");


        HasManyToMany(x => x.DistributedDebts).Table("Distributions]")
                     .ParentKeyColumn("SourceId")
                     .ChildKeyColumn("SubjectId")
                     .Cascade.All();
    }

[Distribtutions]具有字段:

  • SourceId
  • SubjectId。

    SourceId 是父 ChargeOperation.Id 的 ID subjectId 是一个子项(IList DistributedDebts 之一)。 没关系,直到我必须为每个分配操作添加时间并更改表[Distribututions]的字段,如下所示:

    • 来源 ID
    • 主题 ID
    • 操作时间

现在只有一个机会可以做到这一点就是创建映射的分布,打破多对多关系并创建两个一对多关系。 我怎样才能避免它,只需将 timeOfOperation 写入 [Distribututions] 即可。 (我是带着观点来读的,所以这不是阅读的问题)

I have one table or domain entity ChargaOperation, which is mapped to table [ChargeOperation]
and has a many-to-many relations with itself. It's like an offset of client's debts or a tree of distributions (parent and 1 level of children).

ChargeOperation
{
  int Id,
  decimal Amount,
  IList<ChargeOperation>  DistributedDebts // a tree of distributions using many to many mapping
}

relations a realized with a help of [Distribtions] table which has mapping in code and mapped like this:

 public ChargeOperationMap()
    {
        Table("ChargeOperations");
        Id(x => x.Id).GeneratedBy.Identity();
        Map(x => x.Amount).Not.Nullable().Column("Amount");


        HasManyToMany(x => x.DistributedDebts).Table("Distributions]")
                     .ParentKeyColumn("SourceId")
                     .ChildKeyColumn("SubjectId")
                     .Cascade.All();
    }

table [Distribtutions] has fields:

  • SourceId
  • SubjectId.

    SourceId is an id of parent ChargeOperation.Id & subjectId is a child (one of IList DistributedDebts).
    It's was ok, until I have to add a time for each operation of distribution and change field of table [Distribtutions] like this:

    • SourceId
    • SubjectId
    • TimeOfOperation

And now only one oportunity to do it is to create an mapping of Distribtutions, break the many-to-many relations and create two one-to-many relations.
How can I avoid it and just write timeOfOperation to [Distribtutions]. (I read it with a view, so it's not a poblem for reading)

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

压抑⊿情绪 2024-12-21 05:09:08

如果您使用的是 SQL Server,则始终可以将默认值 GetDate() 添加到 timeOfOperation 列。

然后,每当将多对多表插入 timeOfOperation 列时,都会收到当前日期时间。既然您说该列是从视图中读出的,那么您根本不需要映射它。

If you are using SQL Server you could always add a default value of GetDate() to the timeOfOperation column.

Then whenever the many-to-many table is inserted into the timeOfOperation column will receive the current datetime. Since you say that this column is read out from a view then you don't need to map it at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文