If you haven't setup a Fluent NHibernate project before, you should checkout the GettingStarted: Introduction guide.
As mentioned in ClassMap, all mappings are done from inside the constructor of a ClassMap<T>
derived class; so baring that in mind, all examples are going to exclude the surrounding class.
This is the typical mapping for a property that you'll do in most cases, it can be used with any type of property but I'll use a string
for illustrative purposes.
// schema Name varchar(100) // model public string Name { get; set; } // mapping Map(x => x.Name);
You may want to use a different name for the property in your model than what's in your database, here's how to map it:
// schema some_ugly_name varchar(100) // model public string Name { get; set; } // mapping Map(x => x.Name) .ColumnName("some_ugly_name");
If your entity uses a backing field for a property, or some other non-standard design, then you can map it using the Access
property.
// schema Name varchar(100) // model private string name; public string Name { get { return name; } } // mapping Map(x => x.Name) .Access.AsCamelCaseField();
The Access
property can be used to set various combinations of Field
and Property
, with various casings and prefixes.
For example, for the same mapping but with the field called _name
you could use the Prefix.Underscore
overload: Access.AsCamelCaseField(Prefix.Underscore)
.
If you want to be explicit in what the length of your columns can be, then you can use this mapping; this is sometimes useful for if you're generating your schema from your mappings (see Schema Generation).
// schema Name varchar(100) // model public string Name { get; set; } // mapping Map(x => x.Name) .WithLengthOf(100);
If you need to specify that a property is populated from a formula in SQL instead of by a column, you can use the FormulaIs(string)
method.
// schema State int // 1 = active, 2 = archived, 3 = deleted // model public bool IsDeleted { get; private set; } // mapping Map(x => x.IsDeleted) .FormulaIs("State = 3");
You can use this for much more complicated things, as it will accept any valid SQL.
If you need to use a particular IUserType
with a property in your model, then you can specify it with the CustomTypeIs<IUserType>()
method.
Map(x => x.Name) .CustomTypeIs<NameUserType>();
There are also two overloads to this method that accept a Type
instance, and a string
that has the full assembly qualified name of a type.
If you need to change what SQL type the property represents, then you can use the CustomSqlTypeIs(string)
method.
Map(x => x.Name) .CustomSqlTypeIs("NameStringType");
There are several boolean
settings that can be switched on a property, these are all able to be used and negated if need be with the Not
property.
Map(x => x.Count) .AutoNumber(); Map(x => x.Count) .Not.AutoNumber();
This sets the insert
attribute on the property.
Map(x => x.Name) .Nullable(); Map(x => x.Name) .Not.Nullable();
This sets the not-null
attribute on the property (to the opposite, so Nullable
sets not-null
to false
).
Map(x => x.Name) .ReadOnly(); Map(x => x.Name) .Not.ReadOnly();
This sets the insert
and update
attributes on the property at the same time.
Map(x => x.Name) .Unique(); Map(x => x.Name) .Not.Unique();
This sets the unique
attribute on the property.