Skip to content
sunkaixuan edited this page Jun 14, 2019 · 20 revisions

Generating database tables based on entities

1.Create database

//if no exist create datebase SQLSUGAR4XTEST (bin/database/)
db.DbMaintenance.CreateDatabase();

2.Create table

SugarColumn attribute Description

public class CodeFirstTable1
{
        [SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
        public int Id { get; set; }
        public string Name { get; set; }
        [SugarColumn(ColumnDataType = "Nvarchar(255)")]//custom
        public string Text { get; set; }
        [SugarColumn(IsNullable = true)]
        public DateTime CreateTime { get; set; }
}

db.CodeFirst.InitTables(typeof(CodeFirstTable1));//Create table
IsIdentity Create identity
IsPrimaryKey Create primaryKey
ColumnName Custom database column name
IsIgnore Ignore property
IsOnlyIgnoreInsert Insert Ignore property
ColumnDescription Create column description
Length Column length
IsNullable Column is nullable
DecimalDigits decimal(18,2) length=18,DecimalDigits=2
OracleSequenceName Equivalent to identity column
OldColumnName Alert column name,Look at the following examples
IndexGroupNameList Create index,Look at the following examples

OldColumnName

[SugarColumn( OldColumnName ="Name")]
public string  NewName { get; set; }

IndexGroupNameList

[SqlSugar.SugarColumn(IndexGroupNameList = new string[] { "index1" })]
public int V1 { get; set; }
[SqlSugar.SugarColumn(IndexGroupNameList =new string[] { "index1" } )]
public DateTime? V2 { get; set; }

SugarTable attribute Description

db.CodeFirst.InitTables(typeof(CodeFirstTable));

[SugarTable("CodeFirstTable2",TableDescription = "TableDescription")]
public class CodeFirstTable
{
   [SugarColumn(IsPrimaryKey = true)]
   public Guid Id { get; set; }
}
Clone this wiki locally