Recording Computed Properties with NHibernate

I am using NHibernate 2.1.2 + Fluent NHibernate

I have a class and a ContactInfo table. The Name column is encrypted in the database (SQL Server) using EncryptByPassphrase / DecryptByPassphrase .

Below are the relevant bits of the circuit / class / map:

 table ContactInfo( int Id, varbinary(108) Name) public class ContactInfo { public virtual int Id { get; set; } public virtual string Name { get; set; } } public class ContactInfoMap : ClassMap<ContactInfo> { public ContactInfoMap() { Id(x => x.Id); Map(x => x.Name) .Formula("Convert(nvarchar, DecryptByPassPhrase('passphrase', Name))"); } } 

Using the Formula approach, as indicated above, values ​​are correctly read from the database, but NHibernate does not try to insert / update values ​​when saving to the database (which makes sense).

The problem is that I would like to write the Name value using the corresponding EncryptByPassphrase function. I am not sure if NHibernate supports this, and if so, I could not find the right words for an efficient documentation search.

So ... how can I write this computed property back to the database using NHibernate?

Thanks in advance!

+4
source share
1 answer

The property converted to a formula is read-only.

A named query wrapped in the ContactInfoNameUpdater service may be one way to solve the problem.

0
source

Source: https://habr.com/ru/post/1332760/


All Articles