Castle ActiveRecord - Using CreateSchema()

Website
Blog
The Darkside
Posted
09 Dec 2008 at 05:32

Summary

When running unit tests, I often using ActiveRecordStarter.CreateSchema() in my test startups to give me a clean database to work from. Whether or not this should be done by a SQL script or a restore from a backup is for another time. On one such occasion, I started getting a 'binary or string data

Post extract

When running unit tests, I often using ActiveRecordStarter.CreateSchema() in my test startups to give me a clean database to work from. Whether or not this should be done by a SQL script or a restore from a backup is for another time.

On one such occasion, I started getting a 'binary or string data would be truncated' error being thrown and on closer inspection, I noticed it was because of a certain column that was originally nvarchar (max) by design, but had been recreated by ActiveRecord as nvarchar (255).

A quick Google found many folks having this problem and the solutions varied from changing the data type to NTEXT or StringClob, to setting the length of the field to Int32.Max,however, none of them were directly solving the issue at hand.

The easiest way to remedy this is to make use of the SqlType parameter in the Property attribute of ActiveRecord when declaring your properties. 

[Property("DataField", ColumnType="String", NotNull=true, SqlType = "nvarchar (max)")]
public virtual string Data { get ; set; }

Now, when you call ActiveRecord.CreateSchema(), the column will be created correctly.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are 10 types of people in the world, those who can read binary, and those who can't.”