Skip to content
Jon Knopp edited this page Feb 11, 2015 · 5 revisions

I would like my enum to map to an nvarchar field instead of an int. How do I do this?

You can have any field map to nvarchar by decorating the property with the [StringLength(xx)] attribute For example:

public class TestTable {
  [AutoIncrement]
  public int Id { get; set; }

  [StringLength(50)]
  public MyEnumType MyEnumProperty { get; set; }
}

How do I change a sql data type?

SQL types are defined in DbExtensions.cs. Look near the bottom for the TypeToSqlType field.

How do I specify a primary key?

// Auto incrementing primary key Id. The [PrimaryKey] attribute is inferred when using [AutoIncrement]
public class TestTable {
  [AutoIncrement]
  public int Id { get; set; }
}
// Guid primary key
public class TestTable {
  [PrimaryKey]
  [Default("newSequentialId()")]
  public Guid Id { get; set; }
}

How do I specify a foreign key relationship?

public class TestTable {
  [AutoIncrement]
  public int Id { get; set; }
}
public class OtherTestTable {
  [AutoIncrement]
  public int Id { get; set; }
  [References(typeof(TestTable))]
  public int TestTableId { get; set; }
}

How do I control the name of the table?

// By default, we will use plural table names based on the name of the class. 
// You can override this by using the table attribute from the System.ComponentModel.DataAnnotations.Schema namespace
[Table("TestTable")]
public class TestTable {
  [AutoIncrement]
  public int Id { get; set; }
}