Skip to content

Commit

Permalink
Fixed CompositeId.KeyProperty - enums as strings
Browse files Browse the repository at this point in the history
Enums -- by default -- are always mapped as a string with FNH; however,
CompositeId.KeyProperty wasn't following this convention.
  • Loading branch information
jagregory committed May 1, 2010
1 parent c7017fd commit 3a131dc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using FluentNHibernate.Mapping;
using NUnit.Framework;

namespace FluentNHibernate.Testing.DomainModel.Mapping
Expand Down Expand Up @@ -67,6 +69,16 @@ public void KeyManyToOneForeignKey()
.Element("class/composite-id/key-many-to-one").HasAttribute("foreign-key", "fk1");
}

[Test]
public void KeyPropertyEnumShouldBeStringByDefault()
{
new MappingTester<CompIdTarget>()
.ForMapping(c =>
c.CompositeId()
.KeyProperty(x => x.EnumProperty))
.Element("class/composite-id/key-property")
.HasAttribute("type", typeof(GenericEnumMapper<SomeEnum>).AssemblyQualifiedName);
}

[Test]
public void MixedKeyPropertyAndManyToOne()
Expand Down Expand Up @@ -133,8 +145,12 @@ public class CompIdTarget
public virtual ComponentKey Key { get; set; }
public virtual CompIdChild Child { get; set; }
public virtual string DummyProp { get; set; }
public virtual SomeEnum EnumProperty { get; set; }
}

public enum SomeEnum
{}

public class CompIdChild
{
public virtual long ChildId { get; set; }
Expand Down
6 changes: 3 additions & 3 deletions src/FluentNHibernate/Automapping/AutoCompositeIdentityPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public AutoCompositeIdentityPart(IList<Member> mappedMembers)
this.mappedMembers = mappedMembers;
}

protected override CompositeIdentityPart<T> KeyProperty(Member property, string columnName, Action<KeyPropertyPart> customMapping)
protected override CompositeIdentityPart<T> KeyProperty(Member member, string columnName, Action<KeyPropertyPart> customMapping)
{
mappedMembers.Add(property);
mappedMembers.Add(member);

return base.KeyProperty(property, columnName, customMapping);
return base.KeyProperty(member, columnName, customMapping);
}

protected override CompositeIdentityPart<T> KeyReference(Member property, string columnName, Action<KeyManyToOnePart> customMapping)
Expand Down
11 changes: 8 additions & 3 deletions src/FluentNHibernate/Mapping/CompositeIdentityPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,17 @@ public CompositeIdentityPart<T> KeyProperty(Expression<Func<T, object>> expressi
return KeyProperty(member, string.Empty, keyPropertyAction);
}

protected virtual CompositeIdentityPart<T> KeyProperty(Member property, string columnName, Action<KeyPropertyPart> customMapping)
protected virtual CompositeIdentityPart<T> KeyProperty(Member member, string columnName, Action<KeyPropertyPart> customMapping)
{
var type = member.PropertyType;

if (type.IsEnum)
type = typeof(GenericEnumMapper<>).MakeGenericType(type);

var key = new KeyPropertyMapping
{
Name = property.Name,
Type = new TypeReference(property.PropertyType),
Name = member.Name,
Type = new TypeReference(type),
ContainingEntityType = typeof(T)
};

Expand Down

0 comments on commit 3a131dc

Please sign in to comment.