Description:
My object (the XPObject descendant) has a persistent string field. I've noticed that the field size is limited to 100 characters, but I need to store longer text in this field. I have played with the DbType attribute, but could not find the correct syntax to increase the field size. I'm using the MS Access database as a persistent storage.
Answer:
100 characters is the default size of a text property in XPO. It can be changed by adding the Size or DbType attribute to the property's declaration.
DbType arguments are passed to the CREATE TABLE SQL query "as-is". Therefore you should check your database server's SQL syntax for the allowed data types. Thus, MS Access accepts the "varchar(n)" string as a column type. n is an integer value between 1 and 255. If you want XPO to create a longer text column in MS Access, you should use the "LONGTEXT" keyword. Here is some sample code:
C#string _lastName;
[DbType("varchar(200)")]
public string LastName {
get { return _lastName; }
set { SetPropertyValue("LastName", ref _ lastName, value); }
}
string _longMemo;
[DbType("LONGTEXT")]
public string LongMemo {
get { return _longMemo; }
set { SetPropertyValue("LongMemo", ref _longMemo, value); }
}
We advise you against using the DbType attribute since its value is a database type dependent. That is, if you decide to use a different database server as the data store, you will need to change all the DdType attributes. It's best to use the Size attribute instead. In this case, XPO automatically decides which server data type to use to store your object's property.
C#// the same as varchar(255)
// displayed as "Text" in the MS Access table designer
string _firstName;
[Size(255)]
public string FirstName {
get { return _firstName; }
set { SetPropertyValue("FirstName", ref _ firstName, value); }
}
// the same as LONGTEXT
// displayed as "Memo" in the MS Access table designer
string _companyName;
[Size(500)]
public string CompanyName {
get { return _companyName; }
set { SetPropertyValue("CompanyName", ref _companyName, value); }
}
You can use the Size attribute with the SizeAttribute.Unlimited parameter to create a property of the maximum size supported by your database server.
C#string _notes;
[Size(SizeAttribute.Unlimited)]
public string Notes {
get { return _notes; }
set { SetPropertyValue("Notes", ref _notes, value); }
}
Note. The Size attribute is ignored when the DbType is used.
See Also:
SizeAttribute
How to log the SQL queries made by XPO
How does XPO support the delayed loading of objects and properties?