Write your own ORM framework (3): relational mapping configuration-Table attribute

In the previous essay, the encapsulation of the ADO.NET operation database has been completed, and multiple databases have been supported. You only need to specify the database type in the configuration file. This section mainly completes the relationship mapping configuration between objects and database tables.

Let's look at the table name mapping configuration code block 1-1:

[Table(Name="Student")]  
public class StudentEntity  
{  
    //...........省略  
}  

Use the [Table(name = ”Student”)] property to configure on the class, which means that the entity class StudentEntity is mapped to the Student table in the database.
Table attributes need to be written by yourself

TableAttribute.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Rizi.Frame.Orm.Core
{
    /// <summary>
    /// ORM:数据表映射属性
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
    public class TableAttribute : Attribute
    {
        /// <summary>
        /// 使用数据表名初始化TableMapping属性。
        /// </summary>
        /// <param name="name">数据库表名</param>
        public TableAttribute(string name);

        /// <summary>
        /// 使用数据表名和描述信息初始化TableMapping属性
        /// </summary>
        /// <param name="name">数据库表名</param>
        /// <param name="description">描述信息</param>
        public TableAttribute(string name, string description);

        /// <summary>
        /// 描述信息
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 数据库表名
        /// </summary>
        public string Name { get; set; }
    }
}

In the above code, we write the TableAttribute custom attribute class, and then inherit the Attribute custom attribute base class. In specific use, we only need to add [Table(Name=”The name of the table you want to specify” to the class that needs to be configured. "Description")]. The TableAttribute here omits the following Attribute, just use Table. NET will find the TableAttribute class based on the Table name + Atrribute.

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]

This section of attribute configuration represents the usage configuration of the TableAttribute attribute class,

1) AttributeTargets.Class means that it can only be used for classes, so load the attribute on top of the class when using it, such as code block 1-1
2) AllowMultiple means whether you can specify multiple attribute examples for an element, here, for example, whether on StudentEntity Table property can be configured multiple times, we can only configure it once by setting false.
3) Inherited indicates whether the Table property can be inherited. If false is set here, it cannot be inherited.

The Name public attribute is defined in the TableAttribute attribute class, which is used to specify the table name in the database corresponding to the entity configured by the Table attribute.

The Table attribute here has been completed, the next article will continue to introduce custom attributes:
IdAttribute (used to specify which attribute field in the entity class corresponds to the primary key ID in the database table)

Guess you like

Origin blog.csdn.net/coolhe21cn/article/details/79076088