流利的Nhibernate 3映射复合字段(自定义类型)

时间:2011-05-31 09:36:53

标签: fluent-nhibernate composite-id custom-type

全部,我的情景

public class Permission
{
    public virtual Function Function { get; set; }
    public virtual Profile Profile { get; set; }
}

public class MapPermission : ClassMap<Permission>
{
    public MapPermission()
    {
        Table("Permissions".ToUpper());
        CompositeId().KeyProperty(x => x.Function, "FunctionID").KeyProperty(x => x.Profile, "ProfileID");

    }
}

其中Function AND Profile是两个简单映射的实体。当我运行我有这个错误:

无法确定类型:Data.Model.Entities.Function,Data.Model,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null,对于列:NHibernate.Mapping.Column(FunctionID)“}

有没有办法避免这种情况?最终我需要创建一个由两个自定义映射类构成的CompositeID类。如果我将compositeID与int字段一起使用,它就像魅力一样

先谢谢

功能(与配置文件)映射

public class Function
{
    public virtual int ID { get; set; }
    public virtual string Name { get; set; }
}

public class MapFunction : ClassMap<Function>
{
    public MapFunction()
    {
        Table("FUNCTIONS");
        Id(x => x.ID);
        Map(x => x.Name);
    }
}

1 个答案:

答案 0 :(得分:6)

使用 KeyReference 代替 KeyProperty

public class MapPermission : ClassMap<Permission>
{
    public MapPermission()
    {
        Table("Permissions".ToUpper());
        CompositeId()
            .KeyReference(x => x.Function, "FunctionID")
            .KeyReference(x => x.Profile, "ProfileID");

    }
}