forked from fluentmigrator/fluentmigrator
-
Notifications
You must be signed in to change notification settings - Fork 0
Use inbuilt database functions when setting the default value
Daniel Lee edited this page May 19, 2013
·
4 revisions
It is possible to set the default value when creating or altering a column. To just set a value you can use the following fluent syntax:
Create.Table("TestTable").WithColumn("Name").AsString().Nullable().WithDefaultValue("test");
However, you can take advantage of some database functions to set the default value. The SystemMethods enum contains five database functions:
- NewGuid
- NewSequentialId
- CurrentDateTime
- CurrentUTCDateTime
- CurrentUser
These are specific to each database, for example CurrentDateTime calls the GETDATE() function for Sql Server and the now() function for Postgres. By using WithDefault instead of WithDefaultValue, you can pass in one of the enum values.
Create.Table("TestTable").WithColumn("Created").AsDateTime().Nullable().WithDefault(SystemMethods.CurrentDateTime);
- Sql Server all 5 functions implemented
- Postgres 4 functions implemented - NewGuid, CurrentUser, CurrentDateTime and CurrentUTCDateTime (Note: need to run the script share/contrib/uuid-ossp.sql to install the uuid_generate4 function for NewGuid to work. For Postgres 9.1 and 9.2 use this command: CREATE EXTENSION "uuid-ossp"; to install the function.)
- Firebird 2 functions implemented - NewGuid and CurrentDateTime
- Sqlite 1 function implemented - CurrentDateTime
- Oracle 1 function implemented - NewGuid
- MySql 1 function implemented - CurrentDateTime
- Jet no functions implemented
- Getting FluentMigrator
- How to create a Migration
- Fluent Interface
- Migration Runners
- Use inbuilt database functions when setting the default value
- Sql Server Specific Extensions
- Raw Sql Helper for inserting data
- Auto Reversing Migrations
- Resharper File Template
- Transaction Modes for the Migration Runner
- ApplicationContext: Passing parameters to Migrations
- Dealing with Multiple Database Types
- Filter migrations run based on Tags
- Enforce migration version numbering rules
- Create custom metadata for the VersionInfo table