Restricting Columns to Insert or Update

It has been possible since MicroLite 1.2 to restrict a column to be inserted or updated only.

[Column("Created", allowInsert: true, allowUpdate: false)]
public DateTime Created { get; set; }

In the example above, our Created property should not be updated after the record has been inserted. Specifying allowUpdate: false in the column mapping ensures that the property is not included in any SQL generated for an update. Likewise, if allowInsert: false was specified, the column value would not be set upon a new record being inserted.

If both allowInsert and allowUpdate are set to false, the column value can still be included in select statements. This could be useful if you have a column value which is generated by the database and should not be changed by, but needs to be used within an application.

By default, allowInsert and allowUpdate are set to true.

This article was written for version 2.1.0 of the MicroLite framework.

Advertisements

2 thoughts on “Restricting Columns to Insert or Update

    1. trevorpilley Post author

      SqlBuilder only supports creating Select queries, so if you use Select(“*”).From(typeof(Customer)) then it will include all mapped columns in the Select statement regardless of their insert or update permission.

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s