Fluent NHibernate auto mapping: map property from another table's column
Posted
by queen3
on Stack Overflow
See other posts from Stack Overflow
or by queen3
Published on 2009-06-08T14:17:23Z
Indexed on
2010/03/26
2:03 UTC
Read the original article
Hit count: 409
fluent-nhibernate
I'm trying to use S#arp architecture... which includes Fluent NHibernate I'm newbie with (and with NHibernate too, frankly speaking). Auto mapping is used. So I have this:
public class UserAction : Entity
{
public UserAction() { }
[DomainSignature]
[NotNull, NotEmpty]
public virtual string Name { get; set; }
[NotNull, NotEmpty]
public virtual string TypeName { get; private set; }
}
public class UserActionMap : IAutoMappingOverride<UserAction>
{
public void Override(AutoMap<UserAction> mapping)
{
mapping.WithTable("ProgramComponents", m => m.Map(x => x.TypeName));
}
}
Now, table UserActions references table ProgramComponents (many to one) and I want property UserAction.TypeName to have value from db field ProgramComponents.TypeName.
However, the above code fails with
NHibernate.MappingException: Duplicate property mapping of TypeName found in OrderEntry3.Core.UserAction
As far as I understand the problem is that TypeName is already auto-mapped... but I haven't found a way to remove the automatic mapping. Actually I think that my WithTable/Map mapping has to replace the automatic TypeName mapping, but seems like it does not.
I also tried different mapping (names are different but that's all the same):
mapping.WithTable("ProgramComponents", m =>
m.References<ProgramComponent>( x => x.Selector, "ProductSelectorID" )
and still get the same error.
I can overcome this with
mapping.HasOne<ProgramComponent>(x => x.Selector);
but that's not what I exactly wants to do. And I still wonder why the first two methods do not work. I suspect this is because of WithTable.
© Stack Overflow or respective owner