ADO has been completed in the previous article.. NET operating database encapsulation, and supports multiple databases. You only need to specify the database type in the configuration file. This section mainly completes the ing Configuration between objects and database tables.
The following figure shows the ing configuration code block 1-1 of the table name:
[Table (Name = "Student")] public class StudentEntity {// ...... omitted}
Use the [Table (name = "Student")] attribute on the class to configure the stuing between the entity class StudentEntity and the Student Table in the database.
You need to write the Table attribute yourself. Code Block 1-2:
using System;using System.Collections.Generic;using System.Text;namespace System.Orm.CustomAttributes{ [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)] public class TableAttribute : Attribute { private string _Name = string.Empty; public TableAttribute() {} public string Name { get { return _Name; } set { _Name = value; } } }}
In the code above, we compile the TableAttribute custom Attribute class and inherit the Attribute custom Attribute base class, for specific use, we only need to add [Table (Name = "Name of the Table you want to specify")] to the class for which the attribute needs to be configured. Here, TableAttribute is omitted. You can use Table. NET to search for the TableAttribute Class Based on the Table name + Atrribute.
[AttributeUsage (AttributeTargets. Class, AllowMultiple = false, Inherited = false)]
This attribute configuration indicates the usage configuration of the TableAttribute class,
AttributeTargets. Class indicates that it can only be used for classes. Therefore, when used, load the attribute above the Class, such as code block 1-1.
AllowMultiple indicates whether multiple attribute examples can be specified for an element. Here, for example, whether the Table attribute can be configured multiple times on StudentEntity, we can set false to only be configured once.
Inherited indicates whether the Table attribute can be Inherited. If it is set to false, it cannot be Inherited.
The Name public attribute is defined in the TableAttribute class to specify the Table Name in the database corresponding to the object configured in the Table attribute.
The Table attribute has been completed here, and the next article will continue to introduce custom attributes:
IdAttribute (used to specify which attribute field in the object class corresponds to the primary key ID in the database table)