如何使用NHibernate将XML类型列映射到强类型对象属性? [英] How to map XML type column to a strongly typed object property with NHibernate?

查看:96
本文介绍了如何使用NHibernate将XML类型列映射到强类型对象属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下表:

CREATE TABLE [dbo].[Data] (
    [Id]            UNIQUEIDENTIFIER NOT NULL,
    [Data]   XML              NOT NULL,
);

我需要将其映射到对象:

I need to map it to the object:

class Data
{
    public virtual Guid Id {get; set;}
    public virtual StronglyTypedData Data {get; set;}
}

其中StronglyTypedData类似于:

Where, StronglyTypedData is something like:

class StronglyTypedData
{
    public string Name {get; set;}
    public int Number {get; set;}
}

默认情况下,XML列映射到XmlDocument属性,但我希望XML序列化/反序列化到StronglyTypedData属性发生在映射时.

By default, XML columns are mapped to XmlDocument properties, but I would like XML serialization/deserialization to StronglyTypedData property to happen instead at mapping time.

我需要怎么做才能做到这一点?

What do I need to do to accomplish this?

推荐答案

您需要编写一个负责转换的IUserType.

You need to write an IUserType that takes care of the conversion.

您可以从 XmlDocType ,实际上是从原始XML转换为XmlDocument的那个.

You could start from XmlDocType, which is the one actually converting from raw XML to a XmlDocument.

这篇关于如何使用NHibernate将XML类型列映射到强类型对象属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆