Database Reference
In-Depth Information
Figure 12-13. A model with a Candidate entity. The Resume property is of type string in the model but of type XML in
the database
The Resume property of the Candidate entity is of type string in the model, but it is an XML type in the database.
To manipulate the property as if it were of type XML, we'll make the property private and expose a CandidateResume
property as XML.
Select the Resume property and view its properties. Change the setter and getter to private. Next, we need to
expose a new property that will surface the resume as XML. The code in Listing 12-10 provides the details.
With the CandidateResume property, we can manipulate the Resume natively by using the XML API. In Listing 12-10,
we create a strongly-typed resume using XElement class and assign it to the CandidateResume property, which
assigns the original string Resume property inside the setter. After saving the Candidate entity to the database, we later
update the Resume element inside the CandidateResume and update the changes made to the database.
Listing 12-10. Using the CandidateResume Property to Expose the Resume as XML
class Program
{
static void Main(string[] args)
{
RunExample();
}
static void RunExample()
{
using (var context = new EFRecipesEntities())
{
var resume = new XElement("Person",
new XElement("Name", "Robin St.James"),
new XElement("Phone", "817 867-5201"),
new XElement("FirstOffice", "Dog Catcher"),
new XElement("SecondOffice", "Mayor"),
new XElement("ThirdOffice", "State Senator"));
var can = new Candidate
{
Name = "Robin St.James",
CandidateResume = resume
};
context.Candidates.Add(can);
context.SaveChanges();
can.CandidateResume.SetElementValue("Phone", "817 555-5555");
context.SaveChanges();
}
 
Search WWH ::




Custom Search