Search code examples
c#sql-serverorchardcmsdatabase-migrationorchardcms-1.8

How to make a GUID column in Orchard's migrations?


I can make a GUID column through ArcGIS and I can make uniqueidentifier column in SQLSqrver, but Orchard wants to autogenerate the table through migrations.cs. Unfortunately, the GUID column make migration fail, no table is created. I tried to decorate the column in the model with [DatabaseGenerated(DatabaseGeneratedOption.Computed)], but it didn't help. Am I using it wrong? Or do I need something different? I make several other tables, so I would prefer to avoid bigger changes outside the GUID column unless necessary.

Migrations.cs (important parts):

public int Create() {

    SchemaBuilder.CreateTable(typeof(FooRecord).Name,
        table => table
            .Column<int>("Id", column => column.PrimaryKey().Identity())
            .Column<Guid>("GUID", column => column.NotNull().Unique())
            //other columns
        );

    return 1;
}

Model:

public class FooRecord
{
    public virtual int Id { get; set; }
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public virtual Guid GUID { get; set; }
    //other columns
}

Solution

  • Orchard previously didn't support that syntax for guids last I checked (a few years ago) and you had to write it like this:

    .Column("MyGuid", DbType.Guid, column => column.NotNull().Unique())