Grove-based. NET application development tips 1-database primary key setting problems

Source: Internet
Author: User

Grove-based. NET application development tips
 

A few days ago, I downloaded the grove component from the http://grove.91link.com and found that it is good for an Orm. Think

When you want to generate your previous code, the tsql statements are compiled and then called in the generated class. Although Yanyi

I am directly thinking about the idea of dynamically generating SQL when the program is running, but I have never done this.

When using the grove component, I carefully read the tutorial and thought it was a good operation. In fact, this is also the case.

In practice, follow the tutorial to map the class first. Then, call the insert and other methods of the objectoperator object.

. However, after I change the table structure. I found a problem. That is, the default condition.

In this case, the database fields corresponding to the class attribute indicated by the keyfield attribute in the object class must be automatically increasing.

.

If there is a table named customer in the database and there are two columns, the oId is an auto-growth number for the primary key at the same time, deptid

For nvarchar (50), the corresponding ing class is as follows:

Namespace jadesoft. Logistics. systeminfo
{
Using system;
Using Grove. Orm;
Using jadesoft. Common;
[Datatable ("sdepartment")]
Public class Department {
Int _ OID;
String _ deptid = "";

[Keyfield ("oid")]
Public int OID
{
Get {return this. _ OID ;}
Set
{
This. _ OID = value;
}
}
[Datafield ("deptid")]
Public String deptid
{
Get {
Return this. _ deptid;
}
Set {
This. _ deptid = value;
}
}

}
}

It is relatively easy to save the object to the database.

Objectoperator objopt = new objectoperator ();

Objopt. insert (objdepartment.

If we change the structure now. Set the oId of the Department table to the guid type and the primary key, but do not set the default value of the oId column to newid ().

The second column remains unchanged.

The automatically generated ing class is as follows:

Namespace jadesoft. Logistics. systeminfo
{
Using system;
Using Grove. Orm;
Using jadesoft. Common;
[Datatable ("sdepartment")]
Public class Department {
Guid _ OID;
String _ deptid = "";

[Keyfield ("oid")]
Public guid OID
{
Get {return this. _ OID ;}
Set
{
This. _ OID = value;
}
}
[Datafield ("deptid")]
Public String deptid
{
Get {
Return this. _ deptid;
}
Set {
This. _ deptid = value;
}
}

}
}
In this case, an error occurs, as shown in the preceding insert operation. The prompt is that the oId cannot be null. I used the SQL event tracker to track and find that the dynamically generated insert statement is like this.
Insert Department (deptid) values ('aaa'). Of course such statements cannot run normally. I was curious about the principle of generating SQL statements in Grove, So I decompiled the statements. By default, if the attributes modified by the keyfield attribute class in Grove are automatically increasing. So for the first time, I did not generate an error because the insert statement is not generated for the auto-increment column. However, after the structure is changed, I did not specify that this OID property is not auto-increment. There are two types of keyfield in the decompiled code. The decompiled code is as follows.
Using system;

Namespace Grove. ORM
{
[Attributeusageattribute (attributetargets. Property)]
Public class keyfieldattribute: basefieldattribute
{
Private uniqueidtype pktype = uniqueidtype. autoidentitiy;

Public uniqueidtype keytype
{
Get
{
Return pktype;
}

Set
{
Pktype = value;
}
}

Public keyfieldattriname (string columnname): Base (columnname)
{
}
}

}

Namespace Grove. ORM
{
Public Enum uniqueidtype
{
Autoidentitiy = 0,

Otherdefinition = 1,

}

}

I can see how to deal with the current bug. These are not mentioned in the help provided by Grove.
In fact, I only need to indicate to keyfield that it is not automatically increasing.
The modified code is as follows:

Namespace jadesoft. Logistics. systeminfo
{
Using system;
Using Grove. Orm;
Using jadesoft. Common;
[Datatable ("sdepartment")]
Public class Department {
Guid _ OID;
String _ deptid = "";

// The OID attribute indicates that it is the oId column of the primary key column corresponding to the database, and the oId column does not automatically grow.
[Keyfield ("oid", keytype = uniqueidtype. otherdefinition)]
Public guid OID
{
Get {return this. _ OID ;}
Set
{
This. _ OID = value;
}
}
[Datafield ("deptid")]
Public String deptid
{
Get {
Return this. _ deptid;
}
Set {
This. _ deptid = value;
}
}

}
}

After the modification is complete, compile and run the program again. All passed

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.