简体   繁体   English

NHibernate 表更新事件

[英]NHibernate Table Update Event

I have this table mapping (details don't really matter I think):我有这个表映射(我认为细节并不重要):

WithTable("COPACKER_FACILITY");  
Id(x => x.FacilityNumber, "FACILITY_NUM").GeneratedBy.Sequence("FACSEQ");
Map(x => x.FacilityName, "FACILITY_NAME").Not.Nullable().Trimmed();
Map(x => x.AddressLine1, "ADDR1").Not.Nullable().Trimmed();
...
WithTable("FACIL_OTH_AUDIT_INFO", m =>
   {
      m.WithKeyColumn("FACILITY_NUM");
      m.Map(x => x.ProdnShiftsNum, "PRODN_SHIFTS_NUM").Not.Nullable();
      m.Map(x => x.ProdnCapacity, "PRODN_CAPACITY").Not.Nullable();
      m.Map(x => x.ProdnLinesNum, "PRODN_LINES_NUM").Not.Nullable();
      m.Map(x => x.AuditScore, "AUDIT_SCORE");
      m.References(x => x.FacilStatus, "STATUS_IND").Not.Nullable();
   });
HasMany(x => x.ComplianceFlags)
   .KeyColumnNames.Add("FACILITY_NUM")
   .Inverse()
   .Cascade.All();
...

The reason for the one to one table is for audit reasons.一对一表的原因是出于审计原因。 There's a FACIL_OTH_AUDIT_INFO_HIST table that should get a record for every insert and update in the main table.有一个FACIL_OTH_AUDIT_INFO_HIST表应该为主表中的每个插入和更新获取记录。

My question: How can I know when an insert or update happens in that table so I know to insert an audit record?我的问题:如何知道该表中何时发生插入或更新,以便知道插入审计记录?

Many thanks!非常感谢!

I posted the final solution to this problem and thought I'd share我发布了这个问题的最终解决方案,并认为我会分享

http://robtennyson.us/post/2009/08/23/NHibernate-Interceptors.aspx http://robtennyson.us/post/2009/08/23/NHibernate-Interceptors.aspx

+1 to what kvalcanti said... here's another post that I think explains it a little better though (and shows you how to do it without XML configuration.). +1 对 kvalcanti 所说的话……这是另一篇文章,我认为它解释得更好一些(并向您展示了如何在没有 XML 配置的情况下做到这一点。)。 I'm doing what this guy is doing on my project and it's working really well.我正在做这个人在我的项目中所做的事情,而且效果非常好。

http://www.codinginstinct.com/2008/04/nhibernate-20-events-and-listeners.html http://www.codinginstinct.com/2008/04/nhibernate-20-events-and-listeners.html

Caveat: I'm not inserting new objects that need to be saved in this event in my project, which I assume will not be a problem, but I can't say for sure since I'm not doing exactly what you're doing.警告:我不会在我的项目中插入需要在此事件中保存的新对象,我认为这不会有问题,但我不能肯定地说,因为我没有做你正在做的事情.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM