Write the ORM framework by yourself (4): link ing configuration-Id attribute

Source: Internet
Author: User

In the previous article, the Table custom attribute function is completed, and now the Id is completed, because the most important structure of a Table is the Table name, primary key (Id), Column), primary key generation policy. Code Block 1-1: [Table (name = "Student")] public class StudentEntity {private string stuid; [Id (Name = "studentid ", strategy = GenerationType. SEQUENCE)] public string Stuid {get {return stuid;} set {stuid = value ;}} on the Stuid attribute, [Id] indicates that it is in the StudentEntity entity class, the Stuid attribute field corresponds to the primary key ID in the Student table. Name = "studentid" indicates that this attribute corresponds to the studentid column Name in the Student table. If the Name value is not specified or is empty, by default, Stuid is the same as the column name in the corresponding Student table. Strategy = GenerationType. SEQUENCE indicates the primary key generation method, which is automatically increased. The complete code block of the custom property Id is 1-2: using System; using System. collections. generic; using System. text; namespace System. orm. customAttributes {[AttributeUsage (AttributeTargets. field | AttributeTargets. property, AllowMultiple = false, Inherited = false)] public class idattriple: Attribute {private string _ Name = string. empty; private int _ Strategy = GenerationType. INDENTITY; public string Name {get {return _ Name;} Set {_ Name = value ;}} public int Strategy {get {return _ Strategy;} set {_ Strategy = value ;}}} attribute configuration on the IdAttribute class: [AttributeUsage (AttributeTargets. field | AttributeTargets. property, AllowMultiple = false, Inherited = false)] Here, AllowMultiple and Inherited write the orm framework by themselves (3): link ing configuration-Table attributes, this is not a big deal. AttributeTargets. field indicates that the Id attribute can be configured on the private member Field. Code Block 1-3: [Table (name = "Student")] public class StudentEntity {[Id (Name = "studentid ", strategy = GenerationType. SEQUENCE)] private string stuid; // [Id (Name = "studentid", Strategy = GenerationType. SEQUENCE)] public string Stuid {get {return stuid;} set {stuid = value ;}} here we add [Id] to private string stuid; above, it is the same as the property. AttributeTargets. property indicates that the Id Property can be configured on the Property. Note the part/[Id] On Stuid in 1-3. attributeTargets. field | AttributeTargets. add | in the middle of the Property to indicate or, that is, you can configure the Field or Property. A GenerationType class is also used here, with the same effect as enumeration. The source code is as follows: using System; using System. collections. generic; using System. text; namespace System. orm. customAttributes {www.2cto.com public class GenerationType {public const int INDENTITY = 1; // automatically increases public const int SEQUENCE = 2; // SEQUENCE public const int TABLE = 3; // TABLE private GenerationType () {}// private constructor. The object cannot be instantiated}. Now, the Id custom attribute is complete.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.