Showing changes from revision #1 to #2: Added | Removed
There are some shortcuts available for Conventions that can be used inline (much like the old convention style); they're to be used for very simple situations where some people might think defining new types is overkill.
Creating implementations of the interface convention types is always the recommended approach, but if you think otherwise these are available.
There is a ConventionBuilder
class that you can use to define simple conventions. The general rule-of-thumb is if your convention has any logic, or spans multiple lines, then don't use the builders. That being said, they can be useful for simple scenarios.
The ConventionBuilder
defines several static properties, one representing each convention interface. Through each property you can access two methods, Always
and When
. These can be used to create simple conventions that are either always applied, or only applied in particular circumstances.
These inline conventions are applied to every mapping instance of their type, they're handy for catch-all situations.
ConventionBuilder.Class.Always(x => x.WithTable(x.EntityType.Name.ToLower())) ConventionBuilder.Id.Always(x => x.ColumnName("ID"))
These conventions are only applied when the first parameter evaluates to true.
ConventionBuilder.Class.When( x => x.TableName == null, // when this is true x => x.WithTable(x.EntityType.Name + "Table") // do this )
There are some situations that are so obvious that they just cried out for an even simpler shortcut.
Table.Is(x =>x.EntityName.Namex.EntityType.Name + "Table") PrimaryKey.Name.Is(x => "ID") DefaultLazy.AlwaysTrue() DynamicInsert.AlwaysTrue() DynamicUpdate.AlwaysTrue() OptimisticLock.Is(x => x.Dirty()) Cache.Is(x => x.AsReadOnly())
To use these conventions you just need to call Add
on the Fluent Configuration ConventionDiscovery
property.
Fluently.Configure() .Database(/* database config */) .Mappings(m => { m.FluentMappings .AddFromAssemblyOf<Entity>() .ConventionDiscovery.Add(PrimaryKey.Name.Is(x => "ID")); })
The Add
method accepts a params array, so you can specify multiple conventions together.
.ConventionDiscovery.Add( PrimaryKey.Name.Is(x => "ID"), DefaultLazy.AlwaysTrue() )