Question

Im using the Fluent NHibernate together with the automapping functionality. Now im lookin' for something like a configuration, setting, custom attribute - whatever - to declare an entity property as "ReadOnlyFromDb"

In the MsSql database im using a computed column in one of my tables where a value is calculated depending on some other values of the specific data row. Now I have declared this column in the entity class as

public virtual int STATUS { get; private set; }

On getting the specific data of the table everything works fine. The property STATUS is filled correct with the specific value in the datatable.

The problem occures when i try to SaveUpdate() the specific object. I always get the exception

A computed column cannot be the target of an INSERT or UPDATE statement

Which is correct for my understanding - and its how it was supposed to be ;)!

Basically im looking for a configuration, setting, custom attribute - whatever - to say

Hey Fluent NHibernate - get the specific property propertyName but do not insert / update the property propertyName

Is there something like that? Or does a workaround exists for this case? I've searched the fluent nhibernate wiki but have not found a smilliar case.

I hope that someone has already faced and solved this problem!

Here is the code snippet how I create the session (maybe it helps):

    public static ISessionFactory GetNHibernateSession()
    {
        if (nhibernateSession != null)
            return nhibernateSession;

        if (ConfigurationManager.AppSettings[msSqlConnectionString] == null || ConfigurationManager.AppSettings[msSqlConnectionString] == String.Empty)
            throw new NullReferenceException(String.Format("AppSetting '{0}' must not be null!", msSqlConnectionString));

        //nhibernateSession = Fluently.Configure().Database(MsSqlConfiguration.MsSql2005.ConnectionString(ConfigurationManager.AppSettings[msSqlConnectionString]))
        //                              .Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<T_ABWEICHUNGEN>().IgnoreBase<BaseClass>)).BuildSessionFactory();

        nhibernateSession = Fluently.Configure().Database(MsSqlConfiguration.MsSql2005.ConnectionString(ConfigurationManager.AppSettings[msSqlConnectionString]))
                                   .Mappings(m => m.AutoMappings.Add(AutoMap.Assembly(System.Reflection.Assembly.GetExecutingAssembly())
                                   .Override<T_ABWEICHUNGEN>(map =>map.Map(d => d.A08_STATUS_FILE).Length(2147483647))
                                   .IgnoreBase(typeof(BaseClass))
                                   .IgnoreBase(typeof(IDColumn))
                                   .IgnoreBase(typeof(MsSqlQuery))
                                   .IgnoreBase(typeof(MsSqlParamCollection))
                                   .IgnoreBase(typeof(AbweichungStatus))
                                   )).BuildSessionFactory();
        return nhibernateSession;
    }
}

thanks so far for the response - helped me so far. But - is there a way to get this solved more 'dynamicly'?

For example:

I want to declare a custom attribute called [ReadOnlyDbField] now declare all properties of the entity with this cusomt attribute to say: Just read this value and do not update / insert it.

Basically i want to say in the configuration

Map all properties with the custom attribute [ReadOnlyDbField] to Not.Insert().Not.Update()

Is there a way to implement this?

Was it helpful?

Solution

.Override<Entity>(map => map.Map(d => d.STATUS).Not.Insert().Not.Update())

update: account for edited question

public class ReadonlyDbFielConvention : AttributePropertyConvention<ReadOnlyDbField>
{
    protected override void Apply(ReadOnlyDbField attribute, IPropertyInstance instance)
    {
        instance.Not.Insert();
        instance.Not.Update();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top