NHibernate 日志表跟踪表的所有更改

发布于 2024-09-17 07:06:39 字数 651 浏览 7 评论 0原文

我有以下数据库结构:

Users:
- UserID
- UserName
- FirstName
- LastName
...

UsersLog:
- UserLogID
- UserID
- UserName
- FirstName
- LastName
...
- DateCreated

每次进行插入或编辑时,日志表都会针对用户插入一条记录。我拥有日志表的原因是,当提交订单时,我可以简单地根据订单记录用户日志 ID,这意味着我拥有订单时的用户数据的表示。我想知道绘制此图的最佳方法是什么?目前我已经映射了两个表,这让我可以说:

var user = new User { ... };
userRepository.Insert(user);

userLogRepository.Insert(new UserLog { User = user, UserName = user.UserName, FirstName = user.FirstName, ..., DateCreated = DateTime.UtcNow });

到目前为止我最大的问题是在插入日志表时必须重新添加每个字段。我知道我可以创建一个构造函数来处理这个问题,但我想知道是否有更好的方法来处理这个问题。如果有人可以提供帮助,我将不胜感激。

谢谢

i have the following db structure:

Users:
- UserID
- UserName
- FirstName
- LastName
...

UsersLog:
- UserLogID
- UserID
- UserName
- FirstName
- LastName
...
- DateCreated

The log table simply inserts a record against the user everytime an insert or edit is made. The reason i have the log table is that when an order is submitted i can simply log the user log id against the order which means i have a representation of the user data at the time of the order. I was wondering what is the best way to map this? Currently i have both tables mapped which allows me to say:

var user = new User { ... };
userRepository.Insert(user);

userLogRepository.Insert(new UserLog { User = user, UserName = user.UserName, FirstName = user.FirstName, ..., DateCreated = DateTime.UtcNow });

My biggest problem so far is that i have to re-add every field when inserting into the log table. I know i can create a constructor to handle this but i was wondering if there was a better way of handling this. I'd appreciate it if someone could help.

Thanks

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

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

发布评论

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

评论(1

π浅易 2024-09-24 07:06:39

如果您只关心映射代码,可以使用 Automapper

你的代码看起来像这样(这超出了我的想象,但我认为这是正确的):

// Call on app startup, don't try to set the UserLogId field
Mapper.CreateMap<User, UserLog>()
   .ForMember(log => log.UserLogId, opt => opt.Ignore());;

// call when saving the user
userLogRepository.Insert(Mapper.Map<User, UserLog>());

或者,如果你不喜欢代码所在的位置,你可以考虑使用 审核事件侦听器

If you're just concerned about the mapping code, you could use Automapper

Your code would look something like (this is off the top of my head, but I think it's about right):

// Call on app startup, don't try to set the UserLogId field
Mapper.CreateMap<User, UserLog>()
   .ForMember(log => log.UserLogId, opt => opt.Ignore());;

// call when saving the user
userLogRepository.Insert(Mapper.Map<User, UserLog>());

Alternatively, if you don't like where the code is sitting, you could look at using an Audit Event Listener

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