0

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

Daniel W.
  • 449
  • 10
  • 29

2 Answers2

0

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; }
Homayoun Behzadian
  • 1,053
  • 9
  • 26
0

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.

Karel Frajták
  • 4,389
  • 1
  • 23
  • 34
  • Hello Karel. Thanks for the idea - but i don't know who to implement my needs with the generated method. Maybe i have a blockhead? :) – Daniel W. Apr 04 '12 at 05:13