Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML data type

How do I set EF to use an SQL datatype of XML for an object? Moreover, how does one create SQL computed columns.

We store alot of dynamic data that some folks cause meta data per row/record, it isn't standard and therefore we rely upon the xml data structure and then use computed columns to create so keys that we may use for faster SQL searches.

Quite frankly - an expando object mapping to an xml column would really float our boat.

Thanks in advance.

like image 565
DougS Avatar asked Apr 06 '11 16:04

DougS


People also ask

Does XML has data type?

A data type within an XML document is a type that has been assigned to an element on the instance using the dt:dt attribute, or through an XML Schema, a formal definition of an XML document. In addition, data types can be declared as elements. The XML parser uses the data type information to validate the document.

What is XML Schema data types?

The W3C XML Schema Datatype Specification defines numerous datatypes for validating the element content and the attribute value. These datatypes can be used to validate only the scalar content of elements, and not the non-scalar or mixed content.

Is XML the same as SQL?

SQL is good tabular data -- data that easily fits into rows & columns. XML is good for hierarchical data -- data which has several levels of different sizes. SQL is good for storage & searching. XML is good for transmitting & formatting.

What is XML format example?

xml. It is formatted with tags like HTML tags and other XML-based file types include EDS, FDX, and DAE files. An XML file acts as a database to store the data. The most commonly used example of an XML-based file is RSS Feed.


1 Answers

The original question was:

How do I set EF to use an SQL datatype of XML for an object?

In code first, you can do it like this:

[Column(TypeName="xml")]
public string Foo {get; set;}

Or through the fluent api:

modelBuilder.Entity<MyEntity>()
            .Property(x => x.Foo)
            .HasColumnType("xml");

You still need to interpret the property as a string and you're on your own for converting it to xml within C#. But this way it will get generated as an xml column and you can still perform direct sql queries against it using sql's xml functions.

like image 101
Matt Johnson-Pint Avatar answered Oct 27 '22 17:10

Matt Johnson-Pint