简体   繁体   中英

How can I use my abstract base class in a One to Many in a TPH using Fluent NHibernate automapping?

I am trying to use Giveaway.Giveable to accept any subclasses of Giveable but since Giveable is an abstract base class, it's not actually mapped. I'm using Table per Hierarchy instead of TPT.

I'm including classes to help other users and give more real world examples of NHibernate, please excuse all the code.

Exception: An association from the table Giveaway refers to an unmapped class: Giveable

AutoMap.AssemblyOf<Giveaway>(cfg).UseOverridesFromAssemblyOf<UserMappingOverride>()
                                    .Conventions.Add(
                                        PrimaryKey.Name.Is(x => "ID"),
                                        DefaultLazy.Always(),
                                        DefaultCascade.Delete(),
                                        DynamicInsert.AlwaysTrue(),
                                        DynamicUpdate.AlwaysTrue(),
                                        OptimisticLock.Is(x => x.Dirty()),
                                        ForeignKey.EndsWith("ID")))).ExposeConfiguration(BuildSchema)

public abstract class Giveable
{
    ISet<Giveaway> giveaways;

    public Giveable() : base()
    {
        giveaways = new HashedSet<Giveaway>();
    }

    public virtual ISet<Giveaway> Giveaways
    {
        get { return giveaways; }
        set { giveaways = value; }
    }

    public virtual int ID { get; set; }

    public override bool Equals(object obj)
    {
        if (obj == null)
        {
            return false;
        }
        var toCompareWith = obj as Giveable;
        return toCompareWith != null && ((ID == toCompareWith.ID));
    }

    public override int GetHashCode()
    {
        int toReturn = base.GetHashCode();
        toReturn ^= ID.GetHashCode();
        return toReturn;
    }
}

public class Giveaway
{
    ISet<Entry> entries;

    public Giveaway() : base()
    {
        entries = new HashedSet<Entry>();
        CreatedDate = DateTime.UtcNow;
    }

    public virtual DateTime CreatedDate { get; private set; }

    public virtual DateTime? EndDate { get; set; }

    public virtual ISet<Entry> Entries
    {
        get { return entries; }
        set { entries = value; }
    }
    public virtual Giveable Giveable { get; set; }

    public virtual int ID {get;set;}

    public override bool Equals(object obj)
    {
        if (obj == null)
        {
            return false;
        }
        var toCompareWith = obj as Giveaway;
        return toCompareWith != null && ((ID == toCompareWith.ID));
    }

    public override int GetHashCode()
    {
        int toReturn = base.GetHashCode();
        toReturn ^= ID.GetHashCode();
        return toReturn;
    }
}

public class GiveableMappingOverride : IAutoMappingOverride<Giveable>
    {
        public void Override(AutoMapping<Giveable> mapping)
        {
            mapping.DiscriminateSubClassesOnColumn("GiveableType");
            mapping.Map(x => x.Name).Length(200).Not.Nullable();
            mapping.Map(x => x.ImageName).Length(200).Nullable();
            mapping.Map(x => x.ReleaseDate).Not.Nullable();
            mapping.HasMany(x => x.Giveaways)
                .Fetch.Select()
                .AsSet()
                .Inverse()
                .LazyLoad();
        }
    }

public class GiveawayMappingOverride : IAutoMappingOverride<Giveaway>
    {
        public void Override(AutoMapping<Giveaway> mapping)
        {
            mapping.Map(x => x.ThingID).Length(20).Nullable();
            mapping.Map(x => x.Text).Length(2000).Not.Nullable();
            mapping.Map(x => x.CreatedDate).Not.Nullable();
            mapping.Map(x => x.Platform).Not.Nullable();
            mapping.Map(x => x.Type).Not.Nullable();
            mapping.HasMany(x => x.Entries);
            mapping.References(x => x.Giveable).Not.Nullable();
            mapping.References(x => x.User).Not.Nullable();
            mapping.References(x => x.Winner);
        }
    }

I had to include my abstract class because I'm using it. Giveaway.Giveable.

        public class AppMappingOverride : IAutoMappingOverride<App>
        {
            public void Override(AutoMapping<App> mapping)
            {
                mapping.DynamicUpdate();
                mapping.DynamicInsert();
                mapping.HasManyToMany(x => x.Subscriptions).Inverse();
                mapping.HasManyToMany(x => x.Users).Inverse();
            }
        }

        public class SubscriptionMappingOverride : IAutoMappingOverride<Subscription>
        {
            public void Override(AutoMapping<Subscription> mapping)
            {
                mapping.DynamicInsert();
                mapping.DynamicUpdate();
                mapping.HasManyToMany(x => x.Apps);
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM