0
votes

im using the Fluent NHibernate together with the automapping functionality!

I'm currently using the follwing mapping statement to prevent autogenerated column values to be updated / insert

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

It works fine so far, but now im looking for a way to get this solved more dynamically.

What i would like to do is:

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

Then i want to tell the mapping configuration:

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

Is there a way to get this?

Thanks

Daniel

2

2 Answers

0
votes

Have a look at Generated method

Map(x => x.Status).Generated.Never(); // or Insert() or Always()

For auto application look at conventions, some examples here.

0
votes

You must create an attribute class

public class NoInsertUpdateAttribute : Attribute
{

}

and another class for its convention:

public class NoInsertUpdateConvention : AttributePropertyConvention<NoInsertUpdateAttribute>
{
    protected override void Apply(NoInsertUpdateAttribute attribute, IPropertyInstance instance)
    {
        instance.Not.Insert();
        instance.Not.Update();
    }
}

and add container assembly of NoInsertUpdateConvention class to automap:

var fluentConfiguration = Fluently
            .Configure()
            .Mappings(
                m => {
                    var autoMap = AutoMap
                        .Conventions.AddFromAssemblyOf<NoInsertUpdateConvention>()
                }
            );

finally add attribute to property you do not want to be inserted or updated:

    [NoInsertUpdate]
    public virtual int? AccountID { get; set; }