fluent-nhibernate icon indicating copy to clipboard operation
fluent-nhibernate copied to clipboard

Id override is ignored if a property called 'Id' exists.

Open alex-davidson opened this issue 10 years ago • 3 comments

The following test fails:

[Test]
public void CanOverrideIdProperty()
{
    var model = AutoMap.Source(new StubTypeSource(new[] { typeof(EntityWithAlternateIdProperty) }))
        .Override<EntityWithAlternateIdProperty>(o =>
        {
            o.Id(e => e.AlternateId);
        });

    HibernateMapping hibernateMapping = model.BuildMappings().First();

    ClassMapping classMapping = hibernateMapping.Classes.First();
    ((IdMapping)classMapping.Id).Name.ShouldEqual("AlternateId");
}

public class EntityWithAlternateIdProperty
{
    public int Id { get; set; }
    public int AlternateId { get; set; }

    public string Name { get; set; }
}

It seems that AutoMapper#TryMapProperty considers only whether or not the property has already been seen, rather than considering the type of mappings being generated by the rule. The sequence of events seems to be something like:

  1. Override is applied, creating an IdMapping referring to AlternateId and applied at Layer.Defaults. AlternateId is marked as 'seen'.
  2. Begins applying defaults. Properties are iterated.
  3. The Id property is encountered. It has not yet been mapped, so the mapping steps are run against it.
  4. IdentityStep creates an IdMapping referring to Id and applies it at Layer.Defaults. This obliterates the IdMapping created by the override.

Adding o.Map(e => e.Id); to the override works around the problem because it prevents IdentityStep being executed for that property.

I would expect overrides to be applied at Layer.UserSupplied but this does not appear to be the case. Is this a bug, or intended behaviour?

alex-davidson avatar Jan 29 '15 13:01 alex-davidson

Most likely it's a bug. Let me look into it. What version do you use?

chester89 avatar Mar 01 '15 10:03 chester89

I wrote the test against 30edae28, which was the tip of master at the time (29/01/2015?). We initially discovered the bug in the 1.3.0.734 version of the package.

Thanks!

alex-davidson avatar Mar 04 '15 11:03 alex-davidson

I see, thanks

2015-03-04 14:32 GMT+03:00 Alex Davidson [email protected]:

I wrote the test against 30edae2 https://github.com/jagregory/fluent-nhibernate/commit/30edae28f113a56d29dd24f3df7be3d373d29907, which was the tip of master at the time (29/01/2015?). We initially discovered the bug in the 1.3.0.734 version of the package.

Thanks!

— Reply to this email directly or view it on GitHub https://github.com/jagregory/fluent-nhibernate/issues/299#issuecomment-77142575 .

С уважением, Чермённов Глеб

chester89 avatar Mar 04 '15 12:03 chester89