1

How to map XML column to string property in NHibernate in c#?

The property is defined as :

public virtual string Request { get; protected set; }

In mapping class I have:

Map(o => o.Request).Column("Request").Nullable();

I am getting following exception while selecting data from db:

the data types xml and nvarchar are incompatible in the equal to operator. nhibernate

I hope it has something to do with CustomType<> but dont know how. Please help.

1 Answer 1

1

One way to go is to use XDocument as a property type, and the convention, which will map any property to "NHibernate.Type.XDocType"

Complete solution is described here:

Fluent NHibernate and XML columns

Where we add convention Conventions.Add<XmlTypeConvention>(); and define it as:

public class XmlTypeConvention : IUserTypeConvention
{
    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
        criteria.Expect(x => x.Type == typeof(XDocument));
    }

    public void Apply(IPropertyInstance instance)
    {
        instance.CustomType<NHibernate.Type.XDocType>();
    }
} 

This way we will work with NHibernate dedicated type for xml columns...

Sign up to request clarification or add additional context in comments.

4 Comments

I need the .net property to be 'string' type.
Question was about mapping xml typy to string property, not XDocument
@MichalLevý to have mapping between DB and .NET types in your hands ... non standard way ... use custom types. 5.2.3. Custom value types: "It is relatively easy for developers to create their own value types. For example, you might want to persist properties of type Int64 to VARCHAR columns..."
@RadimKöhler Well, maybe custom type is not needed in this case. Not using Fluent NH but in standard NH xml mapping specifying type="StringClob" does the job...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.