在Fluent NHibernate自动映射覆盖中定义唯一列

在Fluent NHibernate自动映射覆盖中定义唯一列,nhibernate,fluent-nhibernate,automapping,Nhibernate,Fluent Nhibernate,Automapping,我试图使用Fluent NHibernate Automapper覆盖为实体指定一个唯一的列。对于我的CodeType测试类,我希望使Type属性唯一。目标是创建一个与当前保存的代码类型具有相同类型字段的“new CodeType()”,并覆盖在当前实体的顶部 我有以下代码类型类: public class CodeType : SecurableEntity { public virtual string Type { get; set; } public virtual s

我试图使用Fluent NHibernate Automapper覆盖为实体指定一个唯一的列。对于我的CodeType测试类,我希望使Type属性唯一。目标是创建一个与当前保存的代码类型具有相同类型字段的“new CodeType()”,并覆盖在当前实体的顶部

我有以下代码类型类:

public class CodeType : SecurableEntity
{

    public virtual string Type { get; set; }
    public virtual string Description { get; set; }

    /// <summary>
    /// This is a placeholder constructor for NHibernate.
    /// A no-argument constructor must be available for NHibernate to create the object.
    /// </summary>
    public CodeType() { }


}
公共类代码类型:SecurableEntity
{
公共虚拟字符串类型{get;set;}
公共虚拟字符串描述{get;set;}
/// 
///这是NHibernate的占位符构造函数。
///NHibernate必须具有无参数构造函数才能创建对象。
/// 
公共代码类型(){}
}
我有以下CodeTypeMap类:

public class CodeTypeMap : IAutoMappingOverride<CodeType>
{
    public void Override(AutoMapping<CodeType> mapping)
    {
        //Doesn't work. Need a way to specify a column as unique.
        mapping.Map(m => m.Type).Unique();
    }
}
公共类CodeTypeMap:IAutoMappingOverride
{
公共无效替代(自动映射)
{
//不起作用。需要一种方法将列指定为唯一。
Map(m=>m.Type).Unique();
}
}
超越通过以下方式应用于自动地图:

    public AutoPersistenceModel Generate()
    {
        var mappings = AutoMap.AssemblyOf<User>(new AutomappingConfiguration());
        mappings.IgnoreBase<Entity>();
        mappings.IgnoreBase<SecurableEntity>();
        mappings.IgnoreBase(typeof(EntityWithTypedId<>));
        mappings.Conventions.Setup(GetConventions());
        mappings.UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();
        mappings.UseOverridesFromAssemblyOf<UserMap>();
        mappings.UseOverridesFromAssemblyOf<CodeMap>();
        mappings.UseOverridesFromAssemblyOf<CodeTypeMap>();

        return mappings;
    }
public AutoPersistenceModel Generate()
{
var mappings=AutoMap.AssemblyOf(新的AutomappingConfiguration());
mappings.IgnoreBase();
mappings.IgnoreBase();
IgnoreBase(typeof(EntityWithTypedId));
mappings.Conventions.Setup(GetConventions());
mappings.UseOverridesFromAssemblyOf();
mappings.UseOverridesFromAssemblyOf();
mappings.UseOverridesFromAssemblyOf();
mappings.UseOverridesFromAssemblyOf();
返回映射;
}
我希望下面的代码更新任何“type”等于“existingType”的现有记录

SecurableEntityRepository ctr=new SecurableEntityRepository();
CodeType ct=新的CodeType();
ct.type=“existingType”;
ct=中心保存或更新(ct);
如何将类型字段的NHibernate键设置为唯一的


这可能吗?

简短的回答,您需要的是必须在代码中处理的东西,因为有太多的可能性。每次创建新的
代码类型
时,必须检查数据库是否已经存在

SecurableEntityRepository<CodeType> ctr = new SecurableEntityRepository<CodeType>();
CodeType ct = ctr.GetByType("existingType");
if (ct == null)
{
    ct = new CodeType { type = "existingType" };
}
ctr.SaveOrUpdate(ct);

你是说你想这样做:“更新代码类型集类型='existingType'其中类型='type'”?而且我不确定你是否需要所有这些
映射。使用OverridesFromAssemblyof()。如果所有替代都存在于同一程序集中,则只需要其中一个替代。
SecurableEntityRepository<CodeType> ctr = new SecurableEntityRepository<CodeType>();
CodeType ct = ctr.GetByType("existingType");
if (ct == null)
{
    ct = new CodeType { type = "existingType" };
}
ctr.SaveOrUpdate(ct);
SecurableEntityRepository<CodeType> ctr = new SecurableEntityRepository<CodeType>();
CodeType ct = ctr.GetByType("existingType");
if (ct != null)
{
    ctr.Detach(ct);
    ctr.Merge(new CodeType{ type = "existingType" });
}
SecurableEntityRepository<CodeType> ctr = new SecurableEntityRepository<CodeType>();
int ctId = ctr.GetIdByType("existingType");
if (ct != 0)
{
    ctr.Merge(new CodeType{ Id = ctId, type = "existingType" });
}
public AutoPersistenceModel Generate()
{
    return AutoMap.AssemblyOf<User>(new AutomappingConfiguration())
        .IgnoreBase<Entity>()
        .IgnoreBase<SecurableEntity>()
        .IgnoreBase(typeof(EntityWithTypedId<>))
        .Conventions.Setup(GetConventions())
        .UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();
}