Audit information on a relation table with NHibernate
I am using NHibernate on a project. The database is required to have creation date and creator user fields on relation tables but I do not want to map the relations to domains. My db design is like:
school (id, name, credate, creuser)
student (id, name, credate, creuser)
开发者_Go百科school_student(id, school_id, student_id, credate, creuser)
For school_student.id, I use autoincrement, pk, not null, so I do not have to map it. NHibernate automatically manages relations so I do not have to map school_id and student_id. For credate, I can use getdate() function, so I do not have to map this field.
But for creuser field, I cannot think of a way that so I do not have to create a domain class for school_student table. How can I escape creating a domain for school_student table? Is there a way of sending creuser information to database each time NHibernate inserts a recort into this table?
You can use SUSER_SNAME()
as the default for the column if it's a direct connection.
Indirectly (eg via a web site) you'll have to send the value of Context.User.Identity
because the DB engine does not know who the end user is. You'll have to map it in nHibernate.
精彩评论