如何向多对多关系表添加列(操作时间)?
我有一个表或域实体 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用的是 SQL Server,则始终可以将默认值
GetDate()
添加到timeOfOperation
列。然后,每当将多对多表插入
timeOfOperation
列时,都会收到当前日期时间。既然您说该列是从视图中读出的,那么您根本不需要映射它。If you are using SQL Server you could always add a default value of
GetDate()
to thetimeOfOperation
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.