1. What is extended attributes?
Many ORM frameworks support such operations. Take the table in section 1 as an example,
The Subject objects such as TeacherID are usually used in this way.
Subject subject = Subject.FindByID(1); string TeacherName = subject.Teacher.Name;
In this case, it is directly associated with the Teacher object, instead of operating TeacherID. How is this done? Will XCode be automatically completed?
First, we are sure XCode will help us automatically implement these extension attributes. Don't believe it. You can check the Teacher. Biz. cs of the Teacher object, which already has such a piece of code.
How does XCode help us bind these attributes?
1. AccordingConventions are better than configurationsInstead of configuring it to make it easier for many people to get started, it is better to set rules to make it easier for everyone to use. Therefore, there are several rules in the help of XCode code generator.
2. for the above rules, I personally understand that if nvarchar is used for that string, I still like varchar. After all, a bunch of spaces will be automatically added after nvarchar, every time I Trim a string, it's annoying. After all, I didn't plan to make my project cross-database, the difference between MSSQL and MSSQL is at most 2 K and 2005 (In my personal opinion, I hope you can give me some advice)
PS: After the explanation of the Major Stone and patch, I know the error here... the space filled by the space is char and nchar.
The difference between nvarchar and varchar is that the storage method is different.
Varchar is stored by byte, while nvarchar with "n" is stored by character.
For example, varchar (40) can store 40 characters in length. When storing Chinese characters, because one Chinese character is equal to two bytes. therefore, varchar (40) can only store 20 Chinese characters.
Nvarchar (40) can store 40 Chinese characters, that is, 80 characters in length. nvarchar is relative to the stored character type. for example, some characters occupy three bytes.
The trim is (n) char ....
3. In 6th cases, XCode automatically generates database comments as code comments. If XCode is not annotated, it will try to translate automatically (here is the connection to Google translator? I don't think XCode has integrated a dictionary by itself ~~~)
4. to automatically generate the extended attributes, the field naming rules for the table are as follows: (primary table name + primary key field of the primary table ).Subject tableYesTeacherIDAnd haveTeacher tableToIDAs the primary key, the extended attributes can be associated.
5. it seems that some extended queries are based on the index. If you have created a query using TeacherID and Subject. name-based index. It seems that XCode will generate such a query method in the Subject table, FindAllByTeacherIDAndSubjectName (int teacherid, string subjectname );
6. The most important thing is to discard the meaningless top tbl, tb, tab, and tab_Student table names. Student is simple and clear.
Ii. How to Write extended attributes in XCode
As in the above example, the Teacher object already has the extended attribute EntityList <Subject> Subjects; however, the Subject does not seem to automatically generate the extended attribute Teacher for us, here we will write a notebook by ourselves, which is actually very simple.
# Region Extension attribute private Teacher _ Teacher; public Teacher {get {if (_ Teacher = null & _ TeacherID> 0 &&! Dirtys. containsKey ("Teacher") {_ Teacher = Domain. teacher. findByID (_ TeacherID); Dirtys ["Teacher"] = true;} return _ Teacher ;}set {_ Teacher = value ;}# endregion
Here, I will explain why the code writes the extended attributes. According to the understanding of the landlord, Dirtys is a dictionary. Have you checked the Teacher? Why is it used to identify it,
It should be based on this consideration, if Teacher! = Null: it must have been checked by Teacher. However, if the database is still null after Teacher queries, an identifier is required to record it. I have already checked the database pull, even if Teacher = null, you do not need to check and pull it. return null directly. This reduces database queries and increases the Cache hit rate.
After writing it in this way, you can use subject. Teacher on the front end to obtain the attributes of the instructor.
Let's test it.
Add some data to the Teacher table first. Here we found a bug in Section 1. The Phone actually uses the Int type, and the 025 input is changed to 25. The entered mobile Phone number is out of the int range, how to fix this bug? First, modify the database, and then use the Xcode code generator to regenerate the table. then overwrite the two files in the project. Here, Teacher. my. cs is an advantage, and our own code is not afraid of misoperations.
Here, we replace and modify the database,
Generate the following code, which is also generated by both the Entity Data and the entity business, and then two files are generated to overwrite them.
Continue to add data and add materials in subject.
Here, we have added three pieces of data, mainly to test whether XCode is like our conjecture. if it hits the Cache, no need to check the database.
To view the SQL statements, make sure that the two switches in config are enabled. In the second lecture, I copied these statements, but forgot to turn on the switch... Jiong
<Deleetask> <! -- Yes? No? Qi? Use? Tune otters? Try ?,? Mo? Recognize? No? Qi? Use? --> <Add key = "XCode. Debug" value = "true"/> <! -- Yes? No? Lost? Outbound? SQL? Sentence ?,? Mo? Recognize? For aXCode? Try? Open? XCode. Debug --> <add key = "XCode. ShowSQL" value = "true"/> <! -- Set hosts? Set? SQL input? Outbound? ? Single region? Single View? ?,? Mo? Recognize? Null for ?,? SQL input? Outbound? To? When an otter? ? Zhi? Medium D. Why? Juúhuan · Jing 3 Jian Xi? Lost? Outbound? To? ? Point? Outbound ticket renewal? Single? SqlLog category? ? --> <Add key = "XCode. SQLPath" value = ""/> <! -- Yes? No? Qi? Use? Anti-attack? Submit a ticket to the supervisor ,? Mo? Recognize? No? Qi? Use ?. Anti-leech? Can I submit a ticket to another employee? Real harm? Now? ? Over y attacks? Body? Category? Anti-attack? To upgrade? Infrastructure 1 --> <add key = "XCode. negative. enable "value =" true "/> </appSettings> I have to say that my code shader has poor support for Chinese characters ''''
3. Start testing
Directly create a new page named test. aspx for today's test ~~~ I have already written it to half past seven... It's not over... I don't want to write a complete example... take a page for a simple test ..
Protected void Page_Load (object sender, EventArgs e) {Domain. subject subject1 = Domain. subject. findByID (1); Response. write (string. format ("{0} subject, Subject name: {1}, Instructor: {2}, instructor's office: {3}, instructor's phone number: {4} <br/> ", subject1.ID, subject1.Name, subject1.Teacher. name, subject1.Teacher. office, subject1.Teacher. phone); Domain. subject subject2 = Domain. subject. findByID (3); if (subject2.Teacher = null) {Response. write (string. format ("{0} subject, Subject name: {1}, instructor is null", subject2.ID, subject2.Name ));}}
If SQLPath is not configured, you can directly have a log folder under the root directory of the project, and the txt file named after the date is the SQL log. You can check the running SQL
Finally, I found... I guess everything is wrong .... XCode initializes all objects and then hits all objects from the Cache ..... after initialization, no SQL statements are executed ....
Then we found that there will be two Select * from teacher and subject statements in a period of time. It is estimated that the XCode Cache Update time is up, so we will automatically update the latest Cache.
I don't have to worry about the internal implementation of XCode. the basic direction of speculation is correct. You can read the source code of XCode on your own. XCode is open-source...
This section is here. The next section describes how to delete and update the cascade function of XCode ~
This Demo
Http://dl.dbank.com/c0wyn9igvt
XCode Getting Started Guide series:
NewLife. XCode Getting Started Guide NewLife. XCode Getting Started Guide (2) reverse engineering example
NewLife Forum address:
Http://www.53wb.com
Big stone blog:
Http://www.cnblogs.com/nnhy/
NewLife. XCode Development Resource Directory
Http://www.cnblogs.com/asxinyu/archive/2012/06/02/2532210.html