2011-11-07 17 views
1

我想使用Fluent NHibernate Automapper Override为实体指定一个唯一列。对于我的CodeType测试类,我想使Type属性唯一。目标是创建一个“新的CodeType()”,其中与当前保存的CodeType具有相同的类型字段,并将其叠加在当前实体的顶部。在Fluent NHibernate Automap Override中定义唯一列

我有以下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() { } 


} 

我有以下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(); 
    } 
} 

的倍率被施加到自动地图,通过执行以下操作:

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; 
    } 

我想要下面的代码来更新任何现有的记录与“类型”等于“existingTyp E”。

SecurableEntityRepository<CodeType> ctr = new SecurableEntityRepository<CodeType>(); 
CodeType ct = new CodeType(); 
ct.type = "existingType"; 
ct = ctr.SaveOrUpdate(ct); 

如何使NHibernate键类型字段作为唯一?

这可能吗?

+0

你说你想要做这样的事情:“更新CODETYPE集类型=‘existingType’其中type =‘类型’ “? –

+0

另外我不确定你需要所有这些'mappings.UseOverridesFromAssemblyOf ();'。如果所有覆盖项都存在于同一个程序集中,则只需要其中的一个。 –

回答

0

简短的回答,你想要的是你必须在代码中处理,因为有这么多的可能性。每次你创建一个新的CodeType你要检查数据库,如果已经有一个

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 CodeType() { }可以移除或protected CodeType() { },如果没有必要为您的域

  • public AutoPersistenceModel Generate() 
    { 
        return AutoMap.AssemblyOf<User>(new AutomappingConfiguration()) 
         .IgnoreBase<Entity>() 
         .IgnoreBase<SecurableEntity>() 
         .IgnoreBase(typeof(EntityWithTypedId<>)) 
         .Conventions.Setup(GetConventions()) 
         .UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>(); 
    } 
    
相关问题