开发者

Fluent NHibernate automap inheritance with subclass relationship

I am having an issue with using Fluent NHibernate automapping with Inheritance. Below is my entity setup (abbreviated for simplicity). I have configured Fluent NHibernate to create 1 class for the hierarchy with a discriminator column. The automapping appears to be working correctly as when I generate a database, one table is created named "AddressBase" with a discriminator column that signals what type of address each row is.

The problem lies in the face that when I call the method "GetPrimaryBillingAddress()" on the UserAccount class, instead of just querying Billing Addresses, NHibernate is creating a query that looks at both Billing and Shipping Addresses. It doesn't take into account the discriminator at all. I am assuming there is some sort of configuration I can set but have not been able to find anything.

public abstract class AddressBase : ActiveRecord<AddressBase>
{
    public virtual long Id { get; set; }

    public virtual string Address1 { get; set; }

}

public class AddressBilling : AddressBase
{
    public class开发者_运维百科 TypedQuery : ActiveRecordQuery<AddressBilling> { }

    public virtual bool IsPrimary { get; set; }
}

public class AddressShipping : AddressBase
{
    public class TypedQuery : ActiveRecordQuery<AddressShipping> { }

    [Display(Name = "Is Primary")]
    public virtual bool IsPrimary { get; set; }

}

public class UserAccount : ActiveRecord<UserAccount>
{
    public virtual long Id { get; set; }

    public virtual IList<AddressBilling> BillingAddresses { get; set; }
    public virtual IList<AddressShipping> ShippingAddresses { get; set; }

    public UserAccount()
    {
        BillingAddresses = new List<AddressBilling>();
        ShippingAddresses = new List<AddressShipping>();
    }

    public virtual AddressBilling GetPrimaryBillingAddress()
    {
        if (BillingAddresses.Any(x => x.IsPrimary))
        {
            return BillingAddresses.Single(x => x.IsPrimary);
        }

        return BillingAddresses.FirstOrDefault();
    }

    public virtual AddressShipping GetPrimaryShippingAddress()
    {
        if (ShippingAddresses.Any(x => x.IsPrimary)) {
            return ShippingAddresses.Single(x => x.IsPrimary);            
        }

        return ShippingAddresses.FirstOrDefault();
    }

}

UPDATE: Here is the Mapping override functions used in the automapping:

private static FluentConfiguration GetFluentConfiguration(string connectionStringName = "CS")
{
        var autoMapping = AutoMap
            .AssemblyOf<Product>(new Mapping.AutoMappingConfiguration())
            .Conventions.Setup(c =>
            {
                c.Add<Mapping.ForeignKeyConvention>();
                c.Add<Mapping.DiscriminatorConvention>();
            })
            .IgnoreBase<AddressBilling.TypedQuery>()
            .IgnoreBase<AddressShipping.TypedQuery>()
            .IncludeBase<AddressBase>();

        return Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2005.ConnectionString(c => c.FromConnectionStringWithKey(connectionStringName)))
            .Mappings(m => m.AutoMappings.Add(autoMapping));
}

public class AutoMappingConfiguration : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Type type)
    {
        var isStatic = type.IsAbstract && type.IsSealed;

        return type.Namespace == typeof(Entities.Product).Namespace && !isStatic;
    }

    public override bool IsDiscriminated(Type type)
    {

        if (type == (typeof(Entities.AddressBase))) {
            return true;
        }

        return false;
    }

    public override string GetDiscriminatorColumn(Type type)
    {
        return "Type";
    }

public class DiscriminatorConvention : ISubclassConvention
{
    public void Apply(ISubclassInstance instance)
    {
        //Address 
        if (instance.Name == typeof(AddressBilling).AssemblyQualifiedName)
        {
            instance.DiscriminatorValue(Enums.AddressType.BillingAddress);
        } 
        else if (instance.Name == typeof(AddressShipping).AssemblyQualifiedName)
        {
            instance.DiscriminatorValue(Enums.AddressType.ShippingAddress);
        }
    }
}

Thanks!


Please, try to change your class UserAccount like this:

public class UserAccount : ActiveRecord<UserAccount>
{
    public virtual IList<AddressBase> Addresses { get; set; }
    public virtual IList<AddressBilling> BillingAddresses { get {return this.Addresses.OfType<AddressBilling>();} }
    public virtual IList<AddressShipping> ShippingAddresses { get {return this.Addresses.OfType<AddressShipping>();} }
    // ...
}

Of course, only Addresses property should be mapped here.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜