新版本的 PetaPoco 使用特性进行注解的形式来代替的老版本的映射类的形式. 新版本中使用的特性主要包括以下几种:
名称 |
| 用途 |
TableNameAttribute | Class | 指定 POCO 实体类对应的数据库表名称 |
ColumnAttribute | Property | 指定 POCO 实体类属性对应的数据库表列名称 |
PrimaryKeyAttribute | Class | 指定 POCO 实体类对应的数据库表的主键列 |
IgnoreAttribute | Property | 指定 POCO 实体类属性不与数据库表列名称对应 |
ResultColumnAttribute | Property | 指定 POCO 实体类属性作为结果信息,只在查询时使用,插入和更新时不更新该列 |
ExplicitColumnsAttribute | Class | 指定 POCO 实体类属性必须使用 ColumnAttribute 或 ResultColumnAttribute 明确指定。 |
ValueConverterAttribute | Property | 指定 POCO 实体类属性的类型与数据库列的类型的转换 |
TableNameAttribute 数据库表名特性, 只能应用于实体类
- /// <summary>
- /// 该特性用来指定 POCO 实体类所对应的数据库表名
- /// </summary>
- [AttributeUsage(AttributeTargets.Class)]
- public class TableNameAttribute : Attribute
- {
- /// <summary>
- /// 实体类对应的数据库表名称
- /// </summary>
- public string Value { get; private set; }
- /// <summary>
- /// 构造函数
- /// </summary>
- public TableNameAttribute(string tableName)
- {
- Value = tableName;
- }
- }
- // 使用事例
- [TableName("Person")]
- public class Person
- {
- }
ColumnAttribute, 数据库表列名特性, 只能用于类的属性.
- /// <summary>
- /// 该特性用于指定 POCO 实体类的属性对应的数据库表的列
- /// </summary>
- [AttributeUsage(AttributeTargets.Property)]
- public class ColumnAttribute : Attribute
- {
- /// <summary>
- /// 数据库表的列名称
- /// </summary>
- public string Name { get; set; }
- /// <summary>
- /// 列对应的属性如果是 DataTime 类型, 表示是否需要转换为 UTC 时间格式
- /// </summary>
- public bool ForceToUtc { get; set; }
- /// <summary>
- /// 插入数据, 将一些数据根据特定格式进行组合 (暂未理解, 后续添加)
- /// </summary>
- public string InsertTemplate { get; set; }
- /// <summary>
- /// 更新数据, 将一些数据根据特定格式进行组合 (暂未理解, 后续添加)
- /// </summary>
- public string UpdateTemplate { get; set; }
- /// <summary>
- /// 构造函数
- /// </summary>
- public ColumnAttribute()
- {
- ForceToUtc = false;
- }
- /// <summary>
- /// 构造函数
- /// </summary>
- public ColumnAttribute(string name)
- {
- Name = name;
- ForceToUtc = false;
- }
- }
- // 使用事例
- [TableName("Person")]
- public class Person
- {
- [Column("id")]
- public string Id;
- }
PrimaryKeyAttribute 数据库表主键列特性, 只能用于类.
- /// <summary>
- /// 该特性用于指定 POCO 实体类的属性对应的数据库表的列是否表的主键和自增信息
- /// Oracle 数据库还需要指定 Sequence 名称
- /// </summary>
- [AttributeUsage(AttributeTargets.Class)]
- public class PrimaryKeyAttribute : Attribute
- {
- /// <summary>
- /// 数据库表的列名称
- /// </summary>
- public string Value { get; private set; }
- /// <summary>
- /// Sequence 名称
- /// </summary>
- public string SequenceName { get; set; }
- /// <summary>
- /// 主键是否自增列
- /// </summary>
- public bool AutoIncrement { get; set; }
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="primaryKey">The name of the primary key column.</param>
- public PrimaryKeyAttribute(string primaryKey)
- {
- Value = primaryKey;
- AutoIncrement = true;
- }
- }
- // 使用事例
- [TableName("Person")]
- [PrimaryKey("id")]
- public class Person
- {
- [Column("id")]
- public string Id;
- }
IgnoreAttribute 类属性不与数据库表列名不进行对应特性, 只能用于类的属性.
- /// <summary>
- /// 该特性用于指定 POCO 实体类的属性不与数据库表的列进行对应
- /// </summary>
- [AttributeUsage(AttributeTargets.Property)]
- public class IgnoreAttribute : Attribute
- {
- }
- // 使用事例
- [TableName("Person")]
- [PrimaryKey("id")]
- public class Person
- {
- [Column("id")]
- public string Id;
- [Ignore]
- public FullName;
- }
ResultColumnAttribute 数据库表列名特性, 只能用于类的属性.
- /// <summary>
- /// 该特性用于指定 POCO 实体类的属性作为数据库表列的结果信息, 更新和查询操作时不对该列进行操作.
- /// </summary>
- [AttributeUsage(AttributeTargets.Property)]
- public class ResultColumnAttribute : ColumnAttribute
- {
- /// <summary>
- /// 构造函数
- /// </summary>
- public ResultColumnAttribute()
- {
- }
- /// <summary>
- /// 构造函数
- /// </summary>
- public ResultColumnAttribute(string name)
- : base(name)
- {
- }
- }
/ 使用事例
- [TableName("Person")]
- [PrimaryKey("id")]
- public class Person
- {
- [Column("id")]
- public string Id;
- [Ignore]
- public string FullName;
- [Result]
- public Float AnnualSalary{ get {return Salary*14;}}
- }
ExplicitColumnsAttribute POCO 实体类特性, 只能用于类的属性.
- /// <summary>
- /// 该特性用于指定 POCO 实体类的属性使用 ColumnAttribute 或 ResultColumnAttribute 明确指定对应关系.
- /// </summary>
- [AttributeUsage(AttributeTargets.Class)]
- public class ExplicitColumnsAttribute : Attribute
- {
- }
ValueConverterAttribute POCO 实体类属性类型转换特性, 只能用于类的属性
- /// <summary>
- /// 该特性用于指定 POCO 实体类的属性的类型和数据库列类型进行转换
- /// </summary>
- [AttributeUsage(AttributeTargets.Property)]
- public abstract class ValueConverterAttribute : Attribute
- {
- /// <summary>
- /// 属性类型转换为数据库列类型
- /// </summary>
- /// <param name="value">Property value</param>
- /// <returns>Converted database value</returns>
- public abstract object ConvertToDb(object value);
- /// <summary>
- /// 数据库列类型转换为属性类型
- /// </summary>
- /// <param name="value">Database value</param>
- /// <returns>Converted property type value</returns>
- public abstract object ConvertFromDb(object value);
- }
来源: http://www.bubuko.com/infodetail-2622947.html