Skip to content

Annotations

shuxin edited this page Feb 17, 2023 · 4 revisions

实体特性

实体特性位于命名空间 Chloe.Annotations 下。

TableAttribute:

Name:映射的表名。 实体对应的表默认是实体类名,如实体类型名与表名不一致,可通过该特性指定映射的表名。

public class TableAttribute : Attribute
{
    public TableAttribute();
    public TableAttribute(string name);

    public string Name { get; set; }
    public string Schema { get; set; }
}

ColumnAttribute:

Name:映射的列名,如不指定,则默认使用属性名。

IsPrimaryKey:指示列是否为主键,默认为 false。

IsRowVersion:指示列是否为为行版本,默认为 false。

注1:1. 如果一个实体没显示指定主键,Chloe 默认选择名为 Id 的属性(如果存在)作为主键。2. 如果实体没有显式指定任何自增列并且主键为 Int16、Int32 或者 Int64 类型,则会默认主键为自增列。如若不是自增列,需使用特性 NonAutoIncrementAttribute 标记

注2:对于区分 varchar 和 nvarchar 类型的数据库(SqlServer、Oracle等),如果数据字段是 varchar 类型,建议把属性的 DbType 设置为 DbType.AnsiString,否则某些情况下查询会出现类型转换,导致索引失效

public class ColumnAttribute : Attribute
{
    public ColumnAttribute();
    public ColumnAttribute(string name);

    public string Name { get; set; }
    public bool IsPrimaryKey { get; set; }
    public bool IsRowVersion { get; set; }
    public DbType DbType { get; set; }
    public int Size { get; set; }
    public int Scale { get; set; }
    public int Precision { get; set; }
}

AutoIncrementAttribute:

标记属性为自增列。一个实体不能指定多个自增列,否则会报异常提示。

public class AutoIncrementAttribute : Attribute
{
    public AutoIncrementAttribute();
}

NonAutoIncrementAttribute:

标记属性为非自增列。

public class NonAutoIncrementAttribute : Attribute
{
    public NonAutoIncrementAttribute();
}

SequenceAttribute:

标记属性通过序列实现自增,需要指定序列名称。

public class SequenceAttribute : Attribute
{
    public SequenceAttribute(string name);

    public string Name { get; }
}

NotMappedAttribute:

标记属性不映射任何列。

public class NotMappedAttribute : Attribute
{
    public NotMappedAttribute();
}

NavigationAttribute:

标记属性为导航属性,详情请参考 github 上的 demo。 https://github.com/shuxinqin/Chloe/blob/master/src/ChloeDemo

public class NavigationAttribute : Attribute
{
    public NavigationAttribute()
    {
    }
    public NavigationAttribute(string foreignKey)
    {
        this.ForeignKey = foreignKey;
    }

    public string ForeignKey { get; private set; }
}

NotNullAttribute:

标记属性不可为 null。

public class NotNullAttribute : Attribute
{
    public NotNullAttribute();
}

UpdateIgnoreAttribute: 标记更新实体时忽略指定字段。

public class UpdateIgnoreAttribute : Attribute
{
    public UpdateIgnoreAttribute();
}

Fluent Mapping

Chloe.ORM 亦支持 Fluent Mapping,具体参考:Fluent-mapping