0

我正在使用流利的nhibernate,并且我正在尝试将我的抽象基类中的一个属性映射到一个特定的表,该表本身不能存在,这取决于派生类是什么。

public class UnmappedClass : Entity<Guid>
{
    // This class isn't mapped to a table on its own, its mapping depends on the class that uses it
}

public abstract class BaseClass : Entity<Guid>
{
    // mapping depends on derived class, not mapped to anything in base class
    protected IList<UnmappedClass> myList = new List<UnmappedClass>();

    public virtual IEnumerable<UnmappedClass> Stuff
    {
        get { return myList.ToList(); }
    }
}

public class DerivedClass : BaseClass
{
    // At this point, I need to map MyClass in member myList to a table specific to DerivedClass
}

我在覆盖中尝试了以下操作并收到错误Invalid object name 'UnmappedClass'.

mapping.HasMany(Reveal.Member<DerivedClass, IEnumerable<UnmappedClass>>("myList"))
    .Table("TableName")
    .Access.Field()
    .Cascade.AllDeleteOrphan()
    .Inverse();
4

1 回答 1

0

我不完全确定,如果这是你想要的,但是使用泛型呢?这是一个例子:

public abstract class BaseClass<T> : Entity<Guid>
    where T : UnmappedClass
{
    // mapping depends on derived class, not mapped to anything in base class
    protected IList<T> myList = new List<T>();

    public virtual IEnumerable<T> Stuff
    {
        get { return myList.ToList(); }
    }
}

public class DerivedClass : BaseClass<MyClass>
{
}

您的映射类也是一个通用基类:

public abstract class BaseClassMap<TEntity, TList> : ClassMap<TEntity>
    where TEntity : BaseClass
    where TList : UnmappedClass
{
    protected BaseClassMap()
    {
        HasMany(Reveal.Member<TEntity, IEnumerable<TList>>("myList"))
            .Table("TableName")
            .Access.Field()
            .Cascade.AllDeleteOrphan()
            .Inverse();
    }
}

public class DerivedClassMap : BaseClassMap<DerivedClass, MyClass>
{
}

我没有检查代码是否编译。您应该获得一个由 MyClass 表(名为“TableName”)引用的 DerivedClass 表。

于 2013-01-23T17:13:25.497 回答