Taking the class which is marked up for storage, and modifying how it is stored
- (page 6 of 6)
If, as is common, we need to store the information in a more structured way the framework provides a couple of methods.
Method 1: Picking out an individual fields for storage in a seperate column
Take the code from the previous step and add the following attribute to the ProgrammeName property.
[PropertyDatabaseColumn("programme_name")]
This marks the property as being stored in a seperate field.
Now run the following SQL against your database:
drop table television_programme
create table television_programme (xml text NOT NULL, television_programme_id BIGINT NOT NULL, programme_name varchar(200))
ALTER TABLE television_programme ADD CONSTRAINT PK_television_programme PRIMARY KEY (television_programme_id)
Run the programme again and check the results are saved. You will find that the programme_name column has been populated with the value of the field.
Method 2: Storing all fields in seperate columns and not storing the XML blob
Add PropertyDatabaseColumn tags to all the attributes in the TelevisionProgramme class and remove the "xml" from the ClassOverview.
You are left with:
using System;
using Ant.ContentEntry; //needed for core content entry services
using Ant.ContentEntry.Converters; // needed for standard converters
using Ant.ContentEntry.FieldDisplayers; // needed for standard displayers
using Ant.ContentEntry.ClassInformation; // needed for the attributes
namespace HowTos
{
/// class representing a television programme
[ClassOverview("television_programme", "television_programme_id")]
public class TelevisionProgramme
{
string programmeName;
int channel;
DateTime start=DateTime.Now;
int duration=30;
/// Default constructor required by content entry
public TelevisionProgramme()
{
}
/// Gets and sets the name of the television programme
[PropertyInformation(typeof(StringConverter), typeof(InputEntry), true, 0)]
[PropertyLanguage(PropertyLanguage.English, "Programme name", "This is the name of the television programme", "You must specify a programme name")]
[PropertyDatabaseColumn("programme_name")]
public string ProgrammeName
{
get
{
return programmeName;
}
set
{
programmeName = value;
}
}
/// Gets and sets the channel that the programme is on
[PropertyInformation(typeof(Int32Converter), typeof(InputEntry), true, 1)]
[PropertyLanguage(PropertyLanguage.English, "Channel number", "Number of the channel that this program is on", "You must specify a channel number")]
[PropertyDatabaseColumn("channel")]
public int Channel
{
get
{
return channel;
}
set
{
channel = value;
}
}
/// Start time of the programme
[PropertyInformation(typeof(DateTimeConverter), typeof(InputEntry), true, 2)]
[PropertyLanguage(PropertyLanguage.English, "Date of programme", "Date the program is on", "You must specify a date ")]
[PropertyDatabaseColumn("start")]
public DateTime Start
{
get
{
return start;
}
set
{
start = value;
}
}
/// Duration of the programme
[PropertyInformation(typeof(Int32Converter), typeof(InputEntry), true, 3)]
[PropertyLanguage(PropertyLanguage.English, "Duration", "Duration of the programme", "You must specify the duration of the programme")]
[PropertyDatabaseColumn("duration")]
public int Duration
{
get
{
return duration;
}
set
{
duration = value;
}
}
}
}
Now run the following SQL against your database:
drop table television_programme
create table television_programme (television_programme_id BIGINT NOT NULL, programme_name varchar(200) NOT NULL, channel int NOT NULL, start DateTime NOT NULL, duration INT NOT NULL)
ALTER TABLE television_programme ADD CONSTRAINT PK_television_programme PRIMARY KEY (television_programme_id)
On running the code all properties are now written to their own columns, and the XML blob is no longer written out.