Search code examples
c#entity-frameworkef-code-firstentity-framework-6xmltype

Mapping xmltype oracle with entity framework 6 code first annotations


I try use code first with a column of xmltype


In my db:

TIPOS_GRIFOS

ID_TIPOS_GRIFOS : NUMBER(38,0)

DESC_TIPOS_GRIFOS : VARCHAR2(250 BYTE)

CONFIG : XMLTYPE


In my code:

[Table("TIPOS_GRIFOS")]
public class TiposGrifos : BaseEntity
{
    [Column("ID_TIPOS_GRIFOS"), Key]
    public int IdTiposGrifos { get; set;}

    [Column("DESC_TIPOS_GRIFOS")]
    public string DescTiposGrifos
    { get; set; }

    [Column("CONFIG")]
    public string Config
    { get; set; }

    [NotMapped]
    public XElement xmlData
    {
        get { return XElement.Parse(Config); }
        set { Config = value.ToString(); }
    }}

I can execute:

   var tiposGrifo = repository.GetById(1);
   tiposGrifo.xmlData = template;

And this object is fine, with data imported correctly from the db. But when I try context.SaveChanges (), it throw ORA-932 exception.

Thanks.

PD: I tried with

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

and that did not work.


UPDATE

I found the problem:

An "ORA-00932: inconsistent datatypes: expected - got NCLOB" error will be encountered when trying to bind a string that is equal to or greater than 2,000 characters in length to an XMLType column or parameter. [Bug 12630958]

I've tried inserting a small xml (less than 2000 characters), and my code works perfectly. But I need to be able to insert larger xml... Any solution?


Solution

  • Update :

    This is Oracle data provider's Known Issue.So you have to wait until they'll fix this issue.

    1. An "ORA-00932 : inconsistent datatypes" error can be encountered if a string of 2,000 or more characters, or a byte array with 4,000 bytes or more in length, is bound in a WHERE clause of a LINQ/ESQL query. The same error can be encountered if an entity property that maps to a BLOB, CLOB, NCLOB, LONG, LONG RAW, XMLTYPE column is used in a WHERE clause of a LINQ/ESQL query.

    You can read about it under the Entity Framework Tips, Limitations, and Known Issues section on this doc.

    Original Post :

    You can try it with Fluent API as shown below.

    public String Config { get; set; }
    
    public XElement xmlData
    {
        get { return XElement.Parse(Config); }
        set { Config = value.ToString(); }
    }
    
    public partial class XmlEntityMap : EntityTypeConfiguration<XmlEntity>
    {
        public FilterMap()
        {
            this.Property(c => c.Config).HasColumnType("xml");
            this.Ignore(c => c.xmlData);
        }
    }