Skip to content

Commit 33bd219

Browse files
authored
Fix overriding Id for automapping (#671)
Fixes #431 +semver:fix
1 parent 3931d96 commit 33bd219

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
namespace FluentNHibernate.Specs.Automapping.Fixtures;
4+
5+
public class EntityWithDifferentId
6+
{
7+
public virtual int DestinationId { get; set; }
8+
public virtual Guid Id { get; set; }
9+
}

src/FluentNHibernate.Specs/Automapping/OverrideSpecs.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using FluentNHibernate.Specs.ExternalFixtures.Overrides;
99
using Machine.Specifications;
1010
using FluentAssertions;
11+
using FluentNHibernate.MappingModel.Identity;
1112

1213
namespace FluentNHibernate.Specs.Automapping;
1314

@@ -81,6 +82,29 @@ public class when_using_an_automapping_override_to_specify_a_discriminator
8182
static ClassMapping mapping;
8283
}
8384

85+
public class when_using_an_automapping_override_to_specify_a_different_id
86+
{
87+
Establish context = () =>
88+
model = AutoMap.Source(new StubTypeSource(typeof(EntityWithDifferentId)))
89+
.Override<EntityWithDifferentId>(map =>
90+
map.Id(x => x.DestinationId));
91+
92+
Because of = () =>
93+
mapping = model.BuildMappingFor<EntityWithDifferentId>();
94+
95+
It should_map_the_id = () =>
96+
mapping.Id.Should().NotBeNull();
97+
98+
It should_map_id_as_id_mapping = () =>
99+
mapping.Id.Should().BeOfType<IdMapping>();
100+
101+
It should_map_id_as_different_id = () =>
102+
((IdMapping)mapping.Id).Name.Should().Be("DestinationId");
103+
104+
static AutoPersistenceModel model;
105+
static ClassMapping mapping;
106+
}
107+
84108
[Subject(typeof(IAutoMappingOverride<>))]
85109
public class when_using_multiple_overrides_from_different_assemblies
86110
{

src/FluentNHibernate/Automapping/AutoMapping.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,24 @@ void IAutoClasslike.AlterModel(ClassMappingBase mapping)
4242
if (mapping is ClassMapping classMapping)
4343
{
4444
if (providers.Id is not null)
45-
classMapping.Set(x => x.Id, Layer.Defaults, providers.Id.GetIdentityMapping());
45+
classMapping.Set(x => x.Id, Layer.UserSupplied, providers.Id.GetIdentityMapping());
4646

4747
if (providers.NaturalId is not null)
48-
classMapping.Set(x => x.NaturalId, Layer.Defaults, providers.NaturalId.GetNaturalIdMapping());
48+
classMapping.Set(x => x.NaturalId, Layer.UserSupplied, providers.NaturalId.GetNaturalIdMapping());
4949

5050
if (providers.CompositeId is not null)
51-
classMapping.Set(x => x.Id, Layer.Defaults, providers.CompositeId.GetCompositeIdMapping());
51+
classMapping.Set(x => x.Id, Layer.UserSupplied, providers.CompositeId.GetCompositeIdMapping());
5252

5353
if (providers.Version is not null)
54-
classMapping.Set(x => x.Version, Layer.Defaults, providers.Version.GetVersionMapping());
54+
classMapping.Set(x => x.Version, Layer.UserSupplied, providers.Version.GetVersionMapping());
5555

5656
if (providers.Discriminator is not null)
57-
classMapping.Set(x => x.Discriminator, Layer.Defaults, providers.Discriminator.GetDiscriminatorMapping());
57+
classMapping.Set(x => x.Discriminator, Layer.UserSupplied, providers.Discriminator.GetDiscriminatorMapping());
5858

5959
if (Cache.IsDirty)
60-
classMapping.Set(x => x.Cache, Layer.Defaults, ((ICacheMappingProvider)Cache).GetCacheMapping());
60+
classMapping.Set(x => x.Cache, Layer.UserSupplied, ((ICacheMappingProvider)Cache).GetCacheMapping());
6161

62-
classMapping.Set(x => x.Tuplizer, Layer.Defaults, providers.TuplizerMapping);
62+
classMapping.Set(x => x.Tuplizer, Layer.UserSupplied, providers.TuplizerMapping);
6363
}
6464

6565
foreach (var join in providers.Joins)

0 commit comments

Comments
 (0)