Entity Framework with a (SQL Server) xml column

entity-framework sql-server-2008 xml

Question

Has anybody used Entity Framework with XML-typed columns? A string is the thing's response.

When the table column is of the type XML, would the next version of Entity Framework allow XElement types.

Regards.

1
16
4/17/2009 1:28:39 PM

Accepted Answer

Whether typed or not, Entity Framework returns the contents of XML columns as strings, making it impossible to generate an entity from an XML document's content (i.e., its nodes). If you're still interested, read this post since it provides a potential fix for your issue. Though I didn't Try It (linking text)

To respond to Marc's last question, I anticipate seeing the ability to map xml document contents as entities in the next release of EF, just as it does with regular tables and their connections. or at the very least, to make certain elements in the XML document properties.

6
5/14/2009 5:10:21 AM

Popular Answer

I'm currently creating a function to a partial class that matches my model and has a. As a string, a data property.

You basically have a call instance. UtilizeData((data) =>...);

You may read and alter data from that point on inside the lambda expression, and it saves back to the original field for the database. For this, I'd prefer to be able to simply build a property that is connected to an event that updates the relevant field.

More dynamic structure is being crammed into the Data field as XML since it seems more natural in VB. That C# is net. I may lean toward JSON if my app was in C#...

C#

public void UseData(Action<XElement> editor)
{
    var def = XElement.Parse("<Data></Data>");
    XElement data;
    try
    {
        if (String.IsNullOrWhiteSpace(this.Data))
        {
            data = def;
        }
        else
        {
            data = XElement.Parse(this.Data);
        }
    }
    catch (Exception ex)
    {
        data = def;
    }
    editor(data);
    this.Data = data.ToString();
}

VB.Net

Public Sub UseData(editor As Action(Of XElement))
    Dim def = <Data></Data>
    Dim data As XElement
    Try
        If String.IsNullOrWhiteSpace(Me.Data) Then
            data = def
        Else
            data = XElement.Parse(Me.Data)
        End If
    Catch ex As Exception
        data = def
    End Try
    editor(data)
    Me.Data = data.ToString()
End Sub


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow