我在 NHibernate 中有一个类,我希望将属性映射到自定义数据类型。
NHibernate 类看起来像这样
namespace Example1.Models
{
public class Calendar
{
public virtual int ID { get; set; }
public virtual string Workspace { get; set; }
}
}
带有映射文件
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
<class name="Example1.Models.Calendar, Example1" lazy="true" table="Calendar">
<id name="ID">
<generator class="native" />
</id>
<property name="Workspace" />
</class>
</hibernate-mapping>
Workspace 属性中的工作区如下所示
http://site/Morning%20meeting?InstanceID=1, Morning meeting
我可以使用 Uri 类将其映射为 URL,但在某些情况下,我还需要将标题与字符串一起提供
public class Calendar
{
public virtual int ID { get; set; }
private Uri _workspace;
public virtual Uri Workspace
{
get { return _workspace; }
set
{
if (value != null)
{
var workspace = value.ToString().Split(new[] { ',' });
_workspace = new Uri(workspace[0]);
}
}
}
}
例如,我尝试创建一个自定义类并将工作区类型添加到该类
private Workspace _workspace;
public virtual Workspace Workspace
{
get { return _workspace; }
set { _workspace = new Workspace(value.ToString()); }
}
...
public class Workspace
{
public Uri Uri { get; set; }
public string Title { get; set; }
public Workspace(string workspace)
{
if(string.IsNullOrEmpty(workspace))
return;
var workspaceArray = workspace.Split(new[] { ',' });
if (workspaceArray.Length > 0)
Uri = new Uri(workspaceArray[0]);
if (workspaceArray.Length > 1)
Title = workspaceArray[1];
}
}
这将呈现错误
Could not determine type for:Example1.Models.Workspace, Example1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, for columns: NHibernate.Mapping.Column(Workspace)
实施此类自定义的推荐方法是什么?
谢谢