如何使用Fluent NHibernate自动映射在TPH中的一对多中使用我的抽象基类
本文关键字:一对多 我的 基类 抽象 TPH NHibernate Fluent 何使用 映射 | 更新日期: 2023-09-27 18:20:29
我正在尝试使用Giveaway.Giveable来接受Giveable的任何子类,但由于Giveable是一个抽象基类,因此它实际上没有映射。我使用的是"按层次表",而不是TPT。
我加入的课程是为了帮助其他用户,并提供更多关于NHibernate的真实世界示例,请原谅所有的代码。
异常:Giveaway表中的关联引用了未映射的类: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);
}
}
我必须包含我的抽象类,因为我正在使用它。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);
}
}