我无法使界面映射工作,无论如何做到这一点?
public class Event
{
public virtual int EventId { get; set; }
public virtual string Name { get; set; }
public virtual ENEventNodeType EventType { get; set; }
public virtual IEventNode EventNode { get; set; }
}
public interface IEventNode {}
public enum ENEventNodeType : short
{
[Description("Mission")]
Mission = 1,
[Description("Task")]
Task = 2,
[Description("Quiz")]
Quiz = 3
}
public class Mission : IEventNode
{
public virtual int MissionId { get; set; }
public virtual string Name { get; set; }
public virtual IList<Event> EventList { get; set; }
}
public class Task : IEventNode
{
public virtual int TaskId { get; set; }
public virtual string Name { get; set; }
public virtual IList<Event> EventList { get; set; }
}
为什么我这样做: IEventNode可以是20多个不同的类,而Event只能有一个。所以其他19个以上的字段将始终为空。 我试图在同一个字段上设置Id,而EventNodeType显示该字段中的哪个类ID。欢迎替代品
解决方案1)AutoMappingOverride
public class EventMappingOverride : IAutoMappingOverride<Event>
{
public void Override(AutoMapping<Event> mapping)
{
var any = mapping.ReferencesAny(x => x.EventNode)
.EntityIdentifierColumn("EventNodeId")
.EntityTypeColumn("EventType")
.IdentityType<int>()
.MetaType<int>();
foreach (var eventNodeType in typeof(Event).Assembly.GetExportedTypes().Where(typeof(IEventNode).IsAssignableFrom))
{
if (!eventNodeType.IsInterface && !eventNodeType.IsAbstract)
any.AddMetaValue(eventNodeType, eventNodeType.Name);
}
}
}
答案 0 :(得分:1)
public class Event
{
public virtual int EventId { get; set; }
public virtual string Name { get; set; }
public virtual IEventNode EventNode { get; set; }
}
public interface IEventNode { }
// example using FluentNHibernate Mapping
public class EventMap : ClassMap<Event>
{
public EventMap()
{
Id(x => x.EventId);
Map(x => x.Name);
var any = ReferencesAny(x => x.EventNode)
.EntityIdentifierColumn("eventNode_id")
.EntityTypeColumn("EventType")
.IdentityType<int>()
.MetaType<int>();
foreach (var eventNodeType in typeof(Event).Assembly.GetExportedTypes().Where(typeof(IEventNode).IsAssignableFrom))
{
if (!eventNodeType.IsInterface && !eventNodeType.IsAbstract)
any.AddMetaValue(eventNodeType, eventNodeType.Name);
}
}
}
然后做
if (event.EventNode is Mission)
Write("Hey its a mission");
...