-
Notifications
You must be signed in to change notification settings - Fork 1.3k
0.2 CodeFirst
果糖网 edited this page Jul 5, 2024
·
9 revisions
//server=.;uid=sa;pwd=sasa;database=SqlSugar5Demo
//database=SqlSugar5Demo is created after the method is called if it does not exist
db.DbMaintenance.CreateDatabase(); // Oracle does not support this method. You need to manually build the database
Support for all databases
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
Column Name | Description |
---|---|
IsIdentity | Whether to create a self increasing identity |
IsPrimaryKey | Whether to create a primary key identifier |
ColumnName | The name of the database field created (default to entity class property name) |
ColumnDataType | The type of database field to create. Usage 1: "varchar (20)" does not require a length to be set. Usage 2: If this parameter is not set, the system will automatically generate the corresponding database type based on the C # type |
IsIgnore | ORM does not process this column |
ColumnDescription | Notes table comments |
Length | Setting the length to 10 will generate xxx type (10), without parentheses, it will not be set |
IsNullable | Can it be null, default to false |
DecimalDigits | Precision, such as decimal (18,2), length=18, DecimalDigits=2 |
OldColumnName | Used to modify column names, so that columns will not be added or deleted |
Default Value | Default value, used to set field default values for table building |
IsOnlyIgnoreInsert | Take default value when inserting data |