Skip to content

Commit

Permalink
Fix overriding Id for automapping (#671)
Browse files Browse the repository at this point in the history
Fixes #431

+semver:fix
  • Loading branch information
hazzik authored Apr 2, 2024
1 parent 3931d96 commit 33bd219
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace FluentNHibernate.Specs.Automapping.Fixtures;

public class EntityWithDifferentId
{
public virtual int DestinationId { get; set; }
public virtual Guid Id { get; set; }
}
24 changes: 24 additions & 0 deletions src/FluentNHibernate.Specs/Automapping/OverrideSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using FluentNHibernate.Specs.ExternalFixtures.Overrides;
using Machine.Specifications;
using FluentAssertions;
using FluentNHibernate.MappingModel.Identity;

namespace FluentNHibernate.Specs.Automapping;

Expand Down Expand Up @@ -81,6 +82,29 @@ public class when_using_an_automapping_override_to_specify_a_discriminator
static ClassMapping mapping;
}

public class when_using_an_automapping_override_to_specify_a_different_id
{
Establish context = () =>
model = AutoMap.Source(new StubTypeSource(typeof(EntityWithDifferentId)))
.Override<EntityWithDifferentId>(map =>
map.Id(x => x.DestinationId));

Because of = () =>
mapping = model.BuildMappingFor<EntityWithDifferentId>();

It should_map_the_id = () =>
mapping.Id.Should().NotBeNull();

It should_map_id_as_id_mapping = () =>
mapping.Id.Should().BeOfType<IdMapping>();

It should_map_id_as_different_id = () =>
((IdMapping)mapping.Id).Name.Should().Be("DestinationId");

static AutoPersistenceModel model;
static ClassMapping mapping;
}

[Subject(typeof(IAutoMappingOverride<>))]
public class when_using_multiple_overrides_from_different_assemblies
{
Expand Down
14 changes: 7 additions & 7 deletions src/FluentNHibernate/Automapping/AutoMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,24 @@ void IAutoClasslike.AlterModel(ClassMappingBase mapping)
if (mapping is ClassMapping classMapping)
{
if (providers.Id is not null)
classMapping.Set(x => x.Id, Layer.Defaults, providers.Id.GetIdentityMapping());
classMapping.Set(x => x.Id, Layer.UserSupplied, providers.Id.GetIdentityMapping());

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

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

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

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

if (Cache.IsDirty)
classMapping.Set(x => x.Cache, Layer.Defaults, ((ICacheMappingProvider)Cache).GetCacheMapping());
classMapping.Set(x => x.Cache, Layer.UserSupplied, ((ICacheMappingProvider)Cache).GetCacheMapping());

classMapping.Set(x => x.Tuplizer, Layer.Defaults, providers.TuplizerMapping);
classMapping.Set(x => x.Tuplizer, Layer.UserSupplied, providers.TuplizerMapping);
}

foreach (var join in providers.Joins)
Expand Down

0 comments on commit 33bd219

Please sign in to comment.