Castle activerecord notes

Source: Internet
Author: User
Tags format definition

1 Table Column
If your column name is a reserved word of the database, use "'" in the property to declare the column name.

[Property ("'user'")]
Public String user
{
Get {return _ User ;}
Set {_ User = value ;}
}

Do not use status as the column name. It is a reserved word of nhib.pdf. Otherwise, activerecord will throw an exception: ambiguous column name 'status'
You can use enumeration as the column type to create integer columns in the database.

2. When the object is saved to the database
Objects are stored in the database only when the session is flush () or transaction commit ().
Auto save
* When session. Find () is called
* When filter () is called
* Automatically save the required session. flushmode: auto (default)

3. Object relationship
* The relationship cannot be established on a non-primary key.
3.1 belonsto
* Multiple-to-one relationships
* Activerecord is the foreign key used to create a database table for this link.
* For example, if "user" is relative to "department", it is stated on the "user" entity class.
3.2 hasmany
* Represents a one-to-many relationship
* Activerecord is the foreign key used to create a database table for this link.
* For example, if "department" is relative to "user", declare it in the "department" entity class.

[Hasmany (typeof (systemuser)]
Public ilist <systemuser> Users
{
Get {return _ users ;}
Set {_ users = value ;}
}

3.3 hasandbelongstow.gov.cn
* Multiple-to-many relationships
* Activerecord creates an associated table
* If a user is allowed to have multiple roles, the "user" and "role"
3.4 onetoone
* Represents a one-to-one relationship.
* Two entities (database tables) have the same primary keys in the database table. From the table object, you must declare the primary key type as primarykeytype. Foreign.
* For example, "user" and "User address"
3.5 Any and hasmanytoany
* Used to indicate an uncertain one-to-multiple relationship
* For example, the "device video port" may store the port information of multiple devices (DVR/matrix/DVS ).

[Activerecord ()]
Public class devicedvr: activerecordbase, ivideodeivce
{}

[Activerecord ()]
Public class devicedvs: activerecordbase, ivideodeivce
{}

[Any (typeof (guid), incluype = typeof (string), typecolumn = "devicetype", idcolumn = "deviceguid", cascade = cascadeenum. saveupdate)]
Public ivideodeivce videodeivce
{
Get {}
Set {}
}

 

4. object class inheritance
4.1 single table inheritance

* All Parent and Child entity classes are saved in a database table.
* "Dictionary", "device type Dictionary", and "device fault dictionary"

/* Use the type column to indicate the dictionary type of the Data row */
[Activerecord (discriminatorcolumn = "type", discriminatortype = "string", discriminatorvalue = "NONE")]
Public class dictionary: activerecordbase <dictionary>
{}

[Activerecord (discriminatorvalue = "device_type")]
Public class dictionarydevicetype: Dictionary

[Activerecord (discriminatorvalue = "device_failure")]
Public class dictionarydevicefailure: Dictionary

4.2 class Table inheritance

* Each parent-child entity class has a database table, which generates a primary key from the base class.
* For example, "car", "sports car", and "rv"

5. nested dataNested data * supports displaying a set of one or more columns in a database table with data objects in a non-database table.
* For example, "user", "unit", and "Address" have address information in the user and Unit tables. The address class is only a set of multiple columns.
* A column in the database is used to define user-defined data to implement user-defined data types in the database.
. In this way, the format definition of Columns with the same meaning in each entity class can be unified (SQL data type, length, format verification, etc)
. For example, emai address and phone number

6. Delayed LoadingLazy load * Delayed loading ensures that data is loaded from the database only when needed. For example, "department" in the "user" object will not be loaded when "user" object is loaded.
* Enable delayed object loading in this way: [activerecord (lazy = true)]
* Delayed loading is not enabled by default for all other links except belongto.

7. Data Verification
Activerecord provides a data validator to verify the rules of column data
The validator verifies the data format before database operations (insert, update ).
The object class must be inherited from activerecordvalidationbase.
The data validators include:
Validateisunique checks that the column value is unique in the table
Validateregexp uses a regular expression to verify the column data format
The data in the validateemail validation column is in the email address format.
Validatenonempty validation column data is not empty
Validateconfirmation verifies that the two attributes are the same, such as password and Password confirmation.
You can inherit abstractvalidationattribut to implement custom validators.
Data Paging
Query
Simplequery <systemuser> userquery = new simplequery <systemuser> ("from systemuser where State =: State order by name ");
Userquery. setparameter ("State", systemuser. userstate. Valid );
Userquery. setqueryrange (pageindex * pagesize, pagesize );
Systemuser [] users = userquery. Execute ();

Slicefindall
Systemuser [] users = systemuser. slicedfindall (30, 10, null, new icriterion [] {expression. eq ("State", systemuser. userstate. Valid )});

Nhib.pdf user-defined data type
Stores more complex data information (such as a list) in a single column and presents it as an object.
Application Scenario: When you do not want to use a single table to store the complex data information
For example, the list of port numbers for device alarms may be multiple, and they can be stored in a database column in the format of "2, 3, 5"
The object in this column must implement the iusertype interface of nhib.pdf, such as public class deivealarmports: List <int>, iusertype
In this way, attributes are declared in the devicealarm class.
[Property ("alarmports", "activerecordentities. Alarm, activerecordentities")]

Public deivealarmports alarmports {get {...} set {...}}

References and links
Activerecord official document Custom Data Type

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.