using System;
namespace FreeSql.DataAnnotations
{
public class ColumnFluent
{
public ColumnFluent(ColumnAttribute column)
{
_column = column;
}
ColumnAttribute _column;
///
/// 数据库列名
///
public ColumnFluent Name(string value)
{
_column.Name = value;
return this;
}
///
/// 指定数据库旧的列名,修改实体属性命名时,同时设置此参数为修改之前的值,CodeFirst才可以正确修改数据库字段;否则将视为【新增字段】
///
public ColumnFluent OldName(string value)
{
_column.OldName = value;
return this;
}
///
/// 数据库类型,如: varchar(255)
///
public ColumnFluent DbType(string value)
{
_column.DbType = value;
return this;
}
///
/// 主键
///
public ColumnFluent IsPrimary(bool value)
{
_column.IsPrimary = value;
return this;
}
///
/// 自增标识
///
public ColumnFluent IsIdentity(bool value)
{
_column.IsIdentity = value;
return this;
}
///
/// 是否可DBNull
///
public ColumnFluent IsNullable(bool value)
{
_column.IsNullable = value;
return this;
}
///
/// 忽略此列,不迁移、不插入
///
public ColumnFluent IsIgnore(bool value)
{
_column.IsIgnore = value;
return this;
}
///
/// 乐观锁
///
public ColumnFluent IsVersion(bool value)
{
_column.IsVersion = value;
return this;
}
///
/// 类型映射,比如:可将 enum 属性映射成 typeof(string)
///
///
///
public ColumnFluent MapType(Type value)
{
_column.MapType = value;
return this;
}
///
/// 创建表时字段位置,规则如下:
///
/// >0时排前面
///
/// =0时排中间(默认)
///
/// <0时排后面
///
///
///
public ColumnFluent Position(short value)
{
_column.Position = value;
return this;
}
///
/// 该字段是否可以插入,默认值true,指定为false插入时该字段会被忽略
///
///
///
public ColumnFluent CanInsert(bool value)
{
_column.CanInsert = value;
return this;
}
///
/// 该字段是否可以更新,默认值true,指定为false更新时该字段会被忽略
///
///
///
public ColumnFluent CanUpdate(bool value)
{
_column.CanUpdate = value;
return this;
}
///
/// 标记属性为数据库服务器时间(utc/local),在插入的时候使用类似 getdate() 执行
///
///
///
public ColumnFluent ServerTime(DateTimeKind value)
{
_column.ServerTime = value;
return this;
}
///
/// 设置长度,针对 string 类型避免 DbType 的繁琐设置
/// ---
/// StringLength = 100 时,对应 DbType:
/// MySql -> varchar(100)
/// SqlServer -> nvarchar(100)
/// PostgreSQL -> varchar(100)
/// Oracle -> nvarchar2(100)
/// Sqlite -> nvarchar(100)
/// ---
/// StringLength = -1 时,对应 DbType:
/// MySql -> text
/// SqlServer -> nvarchar(max)
/// PostgreSQL -> text
/// Oracle -> nvarchar2(4000)
/// Sqlite -> text
///
public ColumnFluent StringLength(int value)
{
_column.StringLength = value;
return this;
}
///
/// 执行 Insert 方法时使用此值
/// 注意:如果是 getdate() 这种请可考虑使用 ServerTime,因为它对数据库间作了适配
///
///
///
public ColumnFluent InsertValueSql(string value)
{
_column.InsertValueSql = value;
return this;
}
}
}