Is it possible to set column default to SQL Server's NewId()?
Asked Answered
B

1

11

In fluentMigrator, is it possible to set a column default to a function?

Specifically, I'm adding a uniqueidentifier (Guid) column to a SQL Server 2008 table that already contains data, and it's going to be a required field - is it possible to set the default to the NewId() function?

Begrudge answered 23/9, 2011 at 14:3 Comment(0)
B
23

Field defaults for NewId() and GetDate() are available (for SQL Server at least) by referring to either SystemMethods.NewGuid or SystemMethods.CurrentDateTime, for example:

Create
    .Column("incomeIdGuid").OnTable("tblIncome")
    .AsGuid()
    .NotNullable()
    .WithDefaultValue(SystemMethods.NewGuid);
Begrudge answered 23/9, 2011 at 17:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.