本文整理汇总了C#中NHibernate.Mapping.ByCode.ConventionModelMapper.IsOneToMany方法的典型用法代码示例。如果您正苦于以下问题:C# ConventionModelMapper.IsOneToMany方法的具体用法?C# ConventionModelMapper.IsOneToMany怎么用?C# ConventionModelMapper.IsOneToMany使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NHibernate.Mapping.ByCode.ConventionModelMapper
的用法示例。
在下文中一共展示了ConventionModelMapper.IsOneToMany方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConventionsMapping
static void ConventionsMapping(ConventionModelMapper mapper)
{
mapper.IsOneToMany((MemberInfo member, Boolean isLikely) =>
{
Type sourceType = member.DeclaringType;
Type destinationType = member.GetMemberFromDeclaringType().GetPropertyOrFieldType();
//check if the property is of a generic collection type
if ((destinationType.IsGenericCollection() == true) && (destinationType.GetGenericArguments().Length == 1))
{
Type destinationEntityType = destinationType.GetGenericArguments().Single();
//check if the type of the generic collection property is an entity
if (mapper.ModelInspector.IsEntity(destinationEntityType) == true)
{
//check if there is an equivalent property on the target type that is also a generic collection and points to this entity
PropertyInfo collectionInDestinationType = destinationEntityType.GetProperties().Where(x => (x.PropertyType.IsGenericCollection() == true) && (x.PropertyType.GetGenericArguments().Length == 1) && (x.PropertyType.GetGenericArguments().Single() == sourceType)).SingleOrDefault();
if (collectionInDestinationType != null)
{
return (false);
}
}
}
return (true);
});
mapper.IsManyToMany((MemberInfo member, Boolean isLikely) =>
{
//a relation is many to many if it isn't one to many
Boolean isOneToMany = mapper.ModelInspector.IsOneToMany(member);
return (!isOneToMany);
});
mapper.BeforeMapClass += (IModelInspector modelInspector, Type type, IClassAttributesMapper classCustomizer) =>
{
classCustomizer.Id(x =>
{
//set the hilo generator
x.Generator(Generators.HighLow);
});
};
mapper.BeforeMapManyToMany += (IModelInspector modelInspector, PropertyPath member, IManyToManyMapper collectionRelationManyToManyCustomizer) =>
{
Type destinationEntityType = member.LocalMember.GetPropertyOrFieldType().GetGenericArguments().First();
//set the mapping table column names from each source entity name plus the _Id sufix
collectionRelationManyToManyCustomizer.Column(destinationEntityType.Name + "_Id");
};
mapper.BeforeMapSet += (IModelInspector modelInspector, PropertyPath member, ISetPropertiesMapper propertyCustomizer) =>
{
if (modelInspector.IsManyToMany(member.LocalMember) == true)
{
propertyCustomizer.Key(x => x.Column(member.LocalMember.DeclaringType.Name + "_Id"));
Type sourceType = member.LocalMember.DeclaringType;
Type destinationType = member.LocalMember.GetPropertyOrFieldType().GetGenericArguments().First();
String [] names = new Type[] { sourceType, destinationType }.Select(x => x.Name).OrderBy(x => x).ToArray();
//set inverse on the relation of the alphabetically first entity name
propertyCustomizer.Inverse(sourceType.Name == names.First());
//set mapping table name from the entity names in alphabetical order
propertyCustomizer.Table(String.Join("_", names));
}
};
}