/// <summary>
/// Maps the <see cref="PropertyInfo"/> to a Many to One relationship
/// </summary>
/// <returns></returns>
public IRelationshipDef MapManyToOne()
{
if (this.PropertyWrapper.DeclaringType == (Type)null) return null;
if (!MustBeMapped()) return null;
var relatedObjectType = this.PropertyWrapper.PropertyType;
if (this.PropertyWrapper.HasAttribute<AutoMapManyToOneAttribute>())
{
var manyToOneAttribute = this.PropertyWrapper.GetAttribute<AutoMapManyToOneAttribute>();
var specifiedType = manyToOneAttribute.RelatedObjectClassType;
if (specifiedType != null) relatedObjectType = specifiedType.ToTypeWrapper();
}
if (!relatedObjectType.IsBusinessObject)
{
throw new InvalidDefinitionException(string.Format(
"The specified RelatedObjectClassType '{0}' on '{1}.{2}' must be a Business Object",
relatedObjectType, this.PropertyWrapper.DeclaringClassName, this.PropertyWrapper.Name));
}
if (!relatedObjectType.IsOfType(this.PropertyWrapper.UnderlyingPropertyType))
{
throw new InvalidDefinitionException(string.Format(
"The specified RelatedObjectClassType '{0}' on '{1}.{2}' must be assignment compatible with the actual property type '{3}'",
relatedObjectType, this.PropertyWrapper.DeclaringClassName,
this.PropertyWrapper.Name, this.PropertyWrapper.UnderlyingPropertyType));
}
var relDef = new SingleRelationshipDef(this.PropertyWrapper.Name, relatedObjectType.UnderlyingType,
new RelKeyDef(), true, DeleteParentAction.DoNothing);
SetRelationshipType(relDef);
if(this.PropertyWrapper.HasCompulsoryAttribute) relDef.SetAsCompulsory();
relDef.OwningBOHasForeignKey = true;
SetReverseRelationshipName(relDef);
var ownerPropName = GetOwningPropName();
var relatedPropName = GetRelatedPropName(relatedObjectType);
IRelPropDef relPropDef = new RelPropDef(ownerPropName, relatedPropName);
relDef.RelKeyDef.Add(relPropDef);
return relDef;
}