Skip to content

Commit dfc1b31

Browse files
committed
Fix for AutoMapping issue wuth UseUnionSubclassForInheritanceMapping on Overrided mappings
Conflicts: src/FluentNHibernate.Testing/FluentNHibernate.Testing.csproj
1 parent 71ec500 commit dfc1b31

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using FluentNHibernate.Automapping;
2+
using FluentNHibernate.Automapping.TestFixtures.SuperTypes;
3+
using NUnit.Framework;
4+
5+
namespace FluentNHibernate.Testing.Automapping
6+
{
7+
[TestFixture]
8+
public class UnionSubclassConventionTests
9+
{
10+
[Test]
11+
public void DefaultConventionsAreAppliedToUnionSubClasses()
12+
{
13+
new AutoMappingTester<SuperType>(
14+
AutoMap.AssemblyOf<SuperType>()
15+
.Where(x => x.Namespace == typeof(SuperType).Namespace)
16+
.Override<SuperType>(m => m.UseUnionSubclassForInheritanceMapping()))
17+
.Element("class/union-subclass[@name='" + typeof(ExampleClass).AssemblyQualifiedName + "']/many-to-one/column")
18+
.HasAttribute("name", "Parent_id");
19+
//.Element("class/union-subclass").Exists();
20+
}
21+
}
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using FluentNHibernate.Automapping;
2+
using FluentNHibernate.Automapping.TestFixtures.SuperTypes;
3+
using NUnit.Framework;
4+
5+
namespace FluentNHibernate.Testing.Automapping
6+
{
7+
[TestFixture]
8+
public class UnionSubclassTests
9+
{
10+
[Test]
11+
public void WhenOverloadedWithUseUnionSubclassForInheritanceMappingUnionSubclassElementShouldExists()
12+
{
13+
new AutoMappingTester<SuperType>(
14+
AutoMap.AssemblyOf<SuperType>()
15+
.Where(x => x.Namespace == typeof(SuperType).Namespace)
16+
.Override<SuperType>(m => m.UseUnionSubclassForInheritanceMapping()))
17+
.Element("class/union-subclass").Exists();
18+
}
19+
}
20+
}

src/FluentNHibernate.Testing/FluentNHibernate.Testing.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@
124124
<Compile Include="DomainModel\MemberAccessResolverTests.cs" />
125125
<Compile Include="DomainModel\MemberBackingFieldTests.cs" />
126126
<Compile Include="DomainModel\NamingTests.cs" />
127+
<Compile Include="AutoMapping\UnionSubclassConventionTests.cs" />
128+
<Compile Include="AutoMapping\UnionSubclassTests.cs" />
127129
<Compile Include="StubTypeSource.cs" />
128130
<Compile Include="AutoMapping\TestFixtures.cs" />
129131
<Compile Include="Cfg\Db\DB2ConfigurationTester.cs" />
@@ -514,4 +516,4 @@
514516
<Target Name="AfterBuild">
515517
</Target>
516518
-->
517-
</Project>
519+
</Project>

src/FluentNHibernate/Automapping/AutoMapper.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ private void MapInheritanceTree(Type classType, ClassMappingBase mapping, IList<
5858
.Where(x => x.Value != null && x.Value.Type == classType)
5959
.Select(x => x.Key))
6060
{
61-
if (isDiscriminated && !discriminatorSet && mapping is ClassMapping)
61+
var tempMapping = mapping as ClassMapping;
62+
var tempIsNull = tempMapping == null;
63+
if (isDiscriminated && !discriminatorSet && !tempIsNull)
6264
{
6365
var discriminatorColumn = cfg.GetDiscriminatorColumn(classType);
6466
var discriminator = new DiscriminatorMapping
@@ -68,13 +70,20 @@ private void MapInheritanceTree(Type classType, ClassMappingBase mapping, IList<
6870
};
6971
discriminator.AddDefaultColumn(new ColumnMapping { Name = discriminatorColumn });
7072

71-
((ClassMapping)mapping).Discriminator = discriminator;
73+
tempMapping.Discriminator = discriminator;
7274
discriminatorSet = true;
7375
}
7476

7577
SubclassMapping subclassMapping;
7678

77-
if (!isDiscriminated)
79+
if(!tempIsNull && tempMapping.IsUnionSubclass)
80+
{
81+
subclassMapping = new SubclassMapping(SubclassType.UnionSubclass)
82+
{
83+
Type = inheritedClass.Type
84+
};
85+
}
86+
else if(!isDiscriminated)
7887
{
7988
subclassMapping = new SubclassMapping(SubclassType.JoinedSubclass)
8089
{

0 commit comments

Comments
 (0)