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键类型字段作为唯一?
这可能吗?
你说你想要做这样的事情:“更新CODETYPE集类型=‘existingType’其中type =‘类型’ “? –
另外我不确定你需要所有这些'mappings.UseOverridesFromAssemblyOf();'。如果所有覆盖项都存在于同一个程序集中,则只需要其中的一个。 –