views:

427

answers:

2

I am working on a project using Castle Active Record. I stumbled across the "Insert = true" attribute argument on the AR association today, but I couldnt workout what it actually does.

[BelongsTo("UserId",Insert = true)]
public ARUser User {
  get { return mUser; }
  set { mUser = value; }
}

Can someone give me a clue? I couldn't find the answer in the documentation.

A: 

From the documentation - Set to false to ignore this association when inserting entities of this ActiveRecord class.

RKitson
+4  A: 

Yep, you'll find the Insert and Update property on a few AR attributes..

I had to do a little testing to make sure I understood the documentation.

Having both Update and Insert set to false indicates that the property will be readonly to your database access (with a public setter this could get confusing.)

[Property(Insert=false, Update=false)]
public virtual string Name { get; set; }

Having update set to true and insert to false indicates that setting this property and then inserting the element will not set that value in the database.

[Property(Insert=false)]
public virtual DateTime Created { get; set; }

As for usage scenarios, you're on your own.

Sciolist
One usage scenario is if you're using a type-safe enum pattern. You create a class with static properties backed by instances of real objects. This allows you to access the static class properties as if they were enums, but the behind-the-scenes instances are persisted into the database as entities instead of values. This has the advantage that you can add additional fields such as 'Sort Order' and 'Display Name' without losing the facade of an immutable enum.
Daniel T.