Skip to content
果糖网 edited this page Jun 23, 2024 · 9 revisions

1、CreateDatabase

db.DbMaintenance.CreateDatabase(); // Oracle does not support this method. You need to manually build the database

2、Create Table

public class CodeFirstTable1
{
[SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
public int Id { get;  set;  }
public string Name { get;  set;  }
//ColumnDataType is generally used for a single database. If ColumnDatatype is used for multiple libraries, it is not recommended
[SugarColumn(ColumnDataType = "Nvarchar(255)")]
public string Text { get;  set;  }
[SugarColumn(IsNullable = true)]// Can be NULL
public DateTime CreateTime { get;  set;  }
}

/*** Create a single table ***/
db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(CodeFirstTable1)); // Then a table can be created successfully
/*** Manually create multiple tables ***/
db.CodeFirst.SetStringDefaultLength(200)
.InitTables(typeof(CodeFirstTable1),typeof(CodeFirstTable2));



/*** Create tables in batches ***/
// Syntax 1:
Type[] types= Assembly
.LoadFrom("XXX.dll")// If the.dll error, can be replaced by xxx.exe Some generated exe
.gettypes ().where (it=> it.fullname.contains ("OrmTest."))// Namespace filtering, other conditions can be written
.ToArray(); // breakpoint debug whether the required Type, not required filtering

db.CodeFirst.SetStringDefaultLength(200).InitTables(types); // Create a table based on types


// Syntax 2:
Type[] types= typeof(class of any entity class).assembly.GetTypes()
.where (it=> it.fullname.contains ("OrmTest."))// Namespace filters, but you can also write other conditional filters
.ToArray();
db.CodeFirst.SetStringDefaultLength(200).InitTables(types); // Create a table based on types

Clone this wiki locally