开发者

Fluent NHibernate not auto-mapping one-to-many property in base class

开发者 https://www.devze.com 2023-02-20 22:35 出处:网络
Let\'s say I have an abstract base class defined as follows: public abstract class CompanyBase : EntityBase<CompanyBase>

Let's say I have an abstract base class defined as follows:

public abstract class CompanyBase : EntityBase<CompanyBase>
{
    public virtual string Name { get; set; }
    public virtual StreetAddress Address { get; set; }
    public virtual IEnumerable<PhoneNumber> PhoneNumbers { get; set; }
}

Then I create several other classes that derive from this class, like this:

public class CustomerCompany : CompanyBase
{
    // Properties relevant to a customer
}

public class VendorCompany : CompanyBase
{
    // Properties relevant to a vendor
}

Each derived class has its own table — in this example CustomerCompany and VendorCompany. There is no CompanyBase table.

I set up auto-mapping via Fluent NHibernate, ignoring the base开发者_如何转开发 classes, and everything works great except for the PhoneNumbers property. It doesn't show up in the hbm file when exported, and it doesn't get pulled from the database. It seems like it's just overlooking that property. If I put the PhoneNumbers property on the derived class, everything works great.

I haven't been able to find anything on the internet related to this...has anyone seen this? Is there a way to work around it or do I have to move the PhoneNumbers declaration out to the derived classes (and end up with a lot of duplication)?

Update:

I accepted the override answer below, but still maintain that automapping should account for collections without overrides. What I ended up doing to solve this particular issue was go to a single discriminated table rather than a table-per-subclass. Doing so allowed things to automap correctly.


You could always override the convention. Auto-mapping is very particular, and can only detect what it already knows to look for and what you additionally tell it:

mappings
   .Override<CustomerCompany>(m=>m.HasMany(x=>x.PhoneNumbers))
   .Override<VendorCompany>(m=>m.HasMany(x=>x.PhoneNumbers));

Adding this to your automapping config should have it follow as many conventions as it can, but also do as specifically told.


One thing you could try...

Change the type of PhoneNumbers from IEnumerable to IList. I would then expect all your derived classes to inherit the list, without any need for conventions or overrides.

I know from experience that IList is well supported by FNH Automapping - not so sure about IEnumerable.

0

精彩评论

暂无评论...
验证码 换一张
取 消