多对多映射问题 - 子集合总是空的

时间:2010-05-03 20:08:21

标签: c# fluent-nhibernate nhibernate-mapping

我有以下情况。我有一个Movie对象,其中包含一个字符列表,每个字符都有一个人(actor)。另外,我希望actor对象包含一个字符列表。

我在fluent-nhibernate中设置了映射,除了Person.Characters集合总是空的外,一切似乎都运行得很好。奇怪的是,Characters.Person对象被正确填充,并且Movie.Characters集合被正确填充。只是Person.Characters始终保持空白。

以下是我使用的映射:

public class MovieMap : ClassMap<Movie>
{
    public MovieMap()
    {
        Id(x => x.Id, "movie_id")
            .GeneratedBy.Assigned();

        Map(x => x.Title);

        HasMany<Character>(x => x.Characters)
            .KeyColumn("movie_id")
            .Inverse()
            .Cascade.All()
            .Not.LazyLoad();
    }
}

public class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {
        Id(x => x.Id, "person_id")
            .GeneratedBy.Assigned();

        Map(x => x.Name);

        HasMany<Character>(x => x.Characters)
            .KeyColumn("person_id")
            .Inverse()
            .Cascade.All()
            .Not.LazyLoad();
    }
}

public class CharacterMap : ClassMap<Character>
{
    public CharacterMap()
    {
        Id(x => x.Id)
            .GeneratedBy.Native();

        Map(x => x.Name);

        References(x => x.Movie, "movie_id")
            .ForeignKey("movie_id")
            .Cascade.All();

        References(x => x.Person, "person_id")
            .ForeignKey("person_id")
            .Cascade.All();
    }
}

以下是我的课程:

public class Person
{
    public Person(int id)
    {
        Id = id;
        Characters = new List<Character>();
    }

    public virtual long Id { get; private set; }
    public virtual string Name { get; set; }
    public virtual IList<Character> Characters { get; set; }
}

public class Movie
{
    public Movie(int Id)
    {
        Id = id;
        Characters = new List<Character>();
    }

    public virtual long Id { get; private set; }
    public virtual string Title { get; set; }
    public virtual IList<Character> Characters { get; set; }
}

public class Character
{
    public Character()
    {
    }

    public virtual int Id { get; private set; }
    public virtual Movie Movie { get; set; }
    public virtual Person Person { get; set; }
    public virtual string Name { get; set; }
}

1 个答案:

答案 0 :(得分:0)

您是否尝试过检索单个Actor或角色?这些映射的错误可能会无声地失败,看起来像找到的0条记录。