Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NHibernate 3.2代码映射忽略我的IUserType_C#_Nhibernate - Fatal编程技术网

C# NHibernate 3.2代码映射忽略我的IUserType

C# NHibernate 3.2代码映射忽略我的IUserType,c#,nhibernate,C#,Nhibernate,我有一个LocalizedStringclassed,用于存储单个值的本地化。这个概念大致是基于一个概念 我在NHibernate 3.2中使用了新的代码映射概念,但它似乎忽略了IUserType实现,因为当它生成SQL时,它会创建一个具有不同名称和默认字符串NVARCHAR(255)类型的列 我正在尝试映射这个简单的类 public class Region : Entity { /// <summary> /// Initializes a new instanc

我有一个
LocalizedString
classed,用于存储单个值的本地化。这个概念大致是基于一个概念

我在NHibernate 3.2中使用了新的代码映射概念,但它似乎忽略了IUserType实现,因为当它生成SQL时,它会创建一个具有不同名称和默认字符串NVARCHAR(255)类型的列

我正在尝试映射这个简单的类

public class Region : Entity
{
    /// <summary>
    /// Initializes a new instance of the <see cref="Region"/> class.
    /// </summary>
    public Region()
    {
    }

    /// <summary>
    /// Gets or sets the localized name of the <see cref="Region"/>.
    /// </summary>
    public virtual LocalizedString Name { get; set; }
}
此处的
列应称为
名称
,并且应为XML类型。我假定列名来自
LocalizedString
的索引器的名称

这是我的NHibernate配置(它不完整,我正在构建约定)


您不应该在域模型中使用IUserType

实际上,
IUserType
接口应该被称为类似于
IUserTypeMapper
,并且您必须在映射中明确指定它

我建议你重读那篇文章


更新:尝试按约定映射您的类型:

mapper.BeforeMapProperty +=
    (insp, prop, map) =>
    {
        if (/*determine if this is member should be mapped as LocalizedString*/)
            map.Type<LocalizedString>();
    };
mapper.BeforeMapProperty+=
(督察、道具、地图)=>
{
if(/*确定此成员是否应映射为LocalizedString*/)
map.Type();
};

当然,“确定是否…”部分将由您确定,例如以“本地化”开头的属性名、自定义属性或任何您想要的内容。

好的,我喜欢不将其绑定到域模型的概念。您知道如何通过新的NHibernate 3.2按代码约定自动将域模型对象映射到其IUserType映射器吗?@Pierre Alainvigant:发布您的配置代码而不是用户类型,我将尝试查看它可以添加到哪里。我还需要将您的解决方案与
mapper.IsProperty((t,声明)=>testHere)结合起来private static Configuration CreateNHibernateConfiguration()
{
    var cfg = new Configuration();
    cfg.Proxy(p => p.ProxyFactoryFactory<NHibernate.Bytecode.DefaultProxyFactoryFactory>())
        .DataBaseIntegration(db =>
        {
            db.ConnectionStringName = "***";
            db.Dialect<MsSql2008Dialect>();
            db.BatchSize = 500;
        });

    var mapper = new ConventionModelMapper();
    var baseEntityType = typeof(Entity);
    mapper.IsEntity((t, declared) => baseEntityType.IsAssignableFrom(t) && baseEntityType != t && !t.IsInterface);
    mapper.IsRootEntity((t, declared) => baseEntityType.Equals(t.BaseType));

    mapper.BeforeMapClass += (mi, t, map) =>
            {
                map.Table(Inflector.MakePlural(t.Name));
                map.Id(x =>
                    {
                        x.Column(t.Name + "Id");
                    });
            };

    mapper.BeforeMapManyToOne += (insp, prop, map) =>
            {
                map.Column(prop.LocalMember.GetPropertyOrFieldType().Name + "Id");
                map.Cascade(Cascade.Persist);
            };

    mapper.BeforeMapBag += (insp, prop, map) =>
            {
                map.Key(km => km.Column(prop.GetContainerEntity(insp).Name + "Id"));
                map.Cascade(Cascade.All);
            };

    mapper.BeforeMapProperty += (insp, prop, map) =>
            {
                map.NotNullable(true);
            };

    var exportedTypes = baseEntityType.Assembly.GetExportedTypes();
    mapper.AddMappings(exportedTypes.Where(t => t.Namespace.EndsWith("Mappings", StringComparison.Ordinal)));

    var mapping = mapper.CompileMappingFor(exportedTypes.Where(t => t.Namespace.EndsWith("Data", StringComparison.Ordinal)));

    cfg.AddDeserializedMapping(mapping, "MyModel");
    SchemaMetadataUpdater.QuoteTableAndColumns(cfg);

    return cfg;
}
/// <summary>
/// Defines a string that can have a different value in multiple cultures.
/// </summary>
public sealed partial class LocalizedString : IUserType
{
    object IUserType.Assemble(object cached, object owner)
    {
        var value = cached as string;
        if (value != null)
        {
            return LocalizedString.Parse(value);
        }

        return null;
    }

    object IUserType.DeepCopy(object value)
    {
        var toCopy = value as LocalizedString;
        if (toCopy == null)
        {
            return null;
        }

        var localizedString = new LocalizedString();
        foreach (var localizedValue in toCopy.localizedValues)
        {
            localizedString.localizedValues.Add(localizedValue.Key, localizedValue.Value);
        }

        return localizedString;
    }

    object IUserType.Disassemble(object value)
    {
        var localizedString = value as LocalizedString;
        if (localizedString != null)
        {
            return localizedString.ToXml();
        }

        return null;
    }

    bool IUserType.Equals(object x, object y)
    {
        if (x == null && y == null)
        {
            return true;
        }

        if (x == null || y == null)
        {
            return false;
        }

        var localizedStringX = (LocalizedString)x;
        var localizedStringY = (LocalizedString)y;

        if (localizedStringX.localizedValues.Count() != localizedStringY.localizedValues.Count())
        {
            return false;
        }

        foreach (var value in localizedStringX.localizedValues)
        {
            if (!localizedStringY.localizedValues.ContainsKey(value.Key) || localizedStringY.localizedValues[value.Key] == value.Value)
            {
                return false;
            }
        }

        return true;
    }

    int IUserType.GetHashCode(object x)
    {
        if (x == null)
        {
            throw new ArgumentNullException("x");
        }

        return x.GetHashCode();
    }

    bool IUserType.IsMutable
    {
        get { return true; }
    }

    object IUserType.NullSafeGet(System.Data.IDataReader rs, string[] names, object owner)
    {
        if (rs == null)
        {
            throw new ArgumentNullException("rs");
        }

        if (names == null)
        {
            throw new ArgumentNullException("names");
        }

        if (names.Length != 1)
        {
            throw new InvalidOperationException("names array has more than one element. can't handle this!");
        }

        var val = rs[names[0]] as string;

        if (val != null)
        {
            return LocalizedString.Parse(val);
        }

        return null;
    }

    void IUserType.NullSafeSet(System.Data.IDbCommand cmd, object value, int index)
    {
        if (cmd == null)
        {
            throw new ArgumentNullException("cmd");
        }

        var parameter = (DbParameter)cmd.Parameters[index];

        var localizedString = value as LocalizedString;
        if (localizedString == null)
        {
            parameter.Value = DBNull.Value;
        }
        else
        {
            parameter.Value = localizedString.ToXml();
        }
    }

    object IUserType.Replace(object original, object target, object owner)
    {
        throw new NotImplementedException();
    }

    Type IUserType.ReturnedType
    {
        get { return typeof(LocalizedString); }
    }

    NHibernate.SqlTypes.SqlType[] IUserType.SqlTypes
    {
        get { return new[] { new XmlSqlType() }; }
    }
}
mapper.BeforeMapProperty +=
    (insp, prop, map) =>
    {
        if (/*determine if this is member should be mapped as LocalizedString*/)
            map.Type<LocalizedString>();
    };