Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DiscriminatorValue override to AutoMapping #469

Merged
merged 1 commit into from
Sep 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,9 @@ public void JoinedSubclassOverrideShouldOverrideExistingReferencesAny()
[Test]
public void SubclassOverrideShouldOverrideExistingProperty()
{
#pragma warning disable 612,618
var autoMapper = AutoMap.AssemblyOf<ExampleClass>()
.Where(t => t.Namespace == "FluentNHibernate.Automapping.TestFixtures")
.Setup(x => x.IsDiscriminated = type => true)
#pragma warning restore 612,618
.Override<ExampleClass>(t => t.DiscriminateSubClassesOnColumn("Discriminator"))
.Override<ExampleInheritedClass>(c => c.Map(x => x.ExampleProperty).Column("test"));

new AutoMappingTester<ExampleClass>(autoMapper)
Expand Down Expand Up @@ -290,11 +288,9 @@ public void SubclassOverrideShouldOverrideExistingComponent()
[Test]
public void SubclassOverrideShouldOverrideExistingHasMany()
{
#pragma warning disable 612,618
var autoMapper = AutoMap.AssemblyOf<ExampleClass>()
.Where(t => t.Namespace == "FluentNHibernate.Automapping.TestFixtures")
.Setup(x => x.IsDiscriminated = type => true)
#pragma warning restore 612,618
.Override<ExampleClass>(t => t.DiscriminateSubClassesOnColumn("Discriminator"))
.Override<ExampleInheritedClass>(m => m.HasMany(x => x.Children).Inverse());

new AutoMappingTester<ExampleClass>(autoMapper)
Expand All @@ -304,11 +300,9 @@ public void SubclassOverrideShouldOverrideExistingHasMany()
[Test]
public void SubclassOverrideShouldOverrideExistingHasManyToMany()
{
#pragma warning disable 612,618
var autoMapper = AutoMap.AssemblyOf<ExampleClass>()
.Where(t => t.Namespace == "FluentNHibernate.Automapping.TestFixtures")
.Setup(x => x.IsDiscriminated = type => true)
#pragma warning restore 612,618
.Override<ExampleClass>(t => t.DiscriminateSubClassesOnColumn("Discriminator"))
.Override<ExampleInheritedClass>(m => m.HasManyToMany(x => x.Children).Inverse());

new AutoMappingTester<ExampleClass>(autoMapper)
Expand All @@ -319,11 +313,9 @@ public void SubclassOverrideShouldOverrideExistingHasManyToMany()
[Test]
public void SubclassOverrideShouldOverrideExistingHasOne()
{
#pragma warning disable 612,618
var autoMapper = AutoMap.AssemblyOf<ExampleClass>()
.Where(t => t.Namespace == "FluentNHibernate.Automapping.TestFixtures")
.Setup(x => x.IsDiscriminated = type => true)
#pragma warning restore 612,618
.Override<ExampleClass>(t => t.DiscriminateSubClassesOnColumn("Discriminator"))
.Override<ExampleInheritedClass>(m => m.HasOne(x => x.Parent));

new AutoMappingTester<ExampleClass>(autoMapper)
Expand All @@ -334,11 +326,9 @@ public void SubclassOverrideShouldOverrideExistingHasOne()
[Test]
public void SubclassOverrideShouldOverrideExistingReferences()
{
#pragma warning disable 612, 618
var autoMapper = AutoMap.AssemblyOf<ExampleClass>()
.Where(t => t.Namespace == "FluentNHibernate.Automapping.TestFixtures")
.Setup(x => x.IsDiscriminated = type => true)
#pragma warning restore 612,618
.Override<ExampleClass>(t => t.DiscriminateSubClassesOnColumn("Discriminator"))
.Override<ExampleInheritedClass>(m => m.References(x => x.Parent).Access.Field());

new AutoMappingTester<ExampleClass>(autoMapper)
Expand All @@ -348,11 +338,9 @@ public void SubclassOverrideShouldOverrideExistingReferences()
[Test]
public void SubclassOverrideShouldOverrideExistingReferencesAny()
{
#pragma warning disable 612, 618
var autoMapper = AutoMap.AssemblyOf<ExampleClass>()
.Where(t => t.Namespace == "FluentNHibernate.Automapping.TestFixtures")
.Setup(x => x.IsDiscriminated = type => true)
#pragma warning restore 612,618
.Override<ExampleClass>(t => t.DiscriminateSubClassesOnColumn("Discriminator"))
.Override<ExampleInheritedClass>(m =>
m.ReferencesAny(x => x.DictionaryChild)
.EntityIdentifierColumn("one")
Expand All @@ -363,5 +351,18 @@ public void SubclassOverrideShouldOverrideExistingReferencesAny()
.Element("//subclass/any[@name='DictionaryChild']").Exists()
.Element("//subclass/map[@name='DictionaryChild']").DoesntExist();
}


[Test]
public void SubclassOverrideShouldOverrideDiscriminatorValue()
{
var autoMapper = AutoMap.AssemblyOf<ExampleClass>()
.Where(t => t.Namespace == "FluentNHibernate.Automapping.TestFixtures")
.Override<ExampleClass>(t => t.DiscriminateSubClassesOnColumn("Discriminator"))
.Override<ExampleInheritedClass>(m => m.DiscriminatorValue(1));

new AutoMappingTester<ExampleClass>(autoMapper)
.Element("//subclass").HasAttribute("discriminator-value", "1");
}
}
}
10 changes: 10 additions & 0 deletions src/FluentNHibernate/Automapping/AutoMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,5 +237,15 @@ public void KeyColumn(string column)

attributes.Set("Key", Layer.UserSupplied, key);
}

/// <summary>
/// Set the discriminator value, if this entity is in a table-per-class-hierarchy
/// mapping strategy.
/// </summary>
/// <param name="discriminatorValue">Discriminator value</param>
public void DiscriminatorValue(object discriminatorValue)
{
attributes.Set("DiscriminatorValue", Layer.UserSupplied, discriminatorValue);
}
}
}