1

我有这样的实体

public class Person
{
    public virtual int Pkey { get; set; }
    public virtual string Name { get; set; }

    public List<Person> Friends{ get; set; }
}

它的表信息是这样的

create table Person
(
    PKey int not null IDENTITY,
    Name varchar (20),
    primary key (PKey)
)

要获取朋友列表,我正在维护另一个这样的表

Create table Friends
(
     PKey int not null IDENTITY,
     PersonFKey int not null Foreign key references Person(PKey),
     FriendFKey int not null Foreign key references Person(PKey)
)

现在,当我进行如下映射时,我遇到了一些错误(由于映射问题)

 public class PersonMap :  ClassMap<Person>
 {
    public PersonMap()
    {
        Id(x => x.Pkey);
        Map(x => x.Name);
        HasManyToMany(x => x.Friends).Cascade.All().Table("Friends").ParentKeyColumn("PersonFKey");
    }
 }

抛出的异常是,

FluentConfigurationException: "An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail."

除了内在的例外,

InvalidProxyTypeException: The following types may not be used as proxies:

FluentNhibernateLearning.Entities.Person: method get_Friends should be 'public/protected virtual' or 'protected internal virtual'
FluentNhibernateLearning.Entities.Person: method set_Friends should be 'public/protected virtual' or 'protected internal virtual'

谁能帮我指出我缺少的东西?

4

2 回答 2

4

您没有说明错误是什么并且映射与类不匹配,但我认为问题在于您缺少ChildKeyColumn声明。在多对多映射中,您必须声明父键列和子键列;父键是包含集合的类的主键,子键是集合中类的主键。

此外,您几乎从不希望级联多对多,因为这会导致删除删除所有相关实体。也就是说,删除一个人会删除他们所有的朋友。

 public class IntermediaryMap :  ClassMap<Intermediary>
 {
    public IntermediaryMap()
    {
        Id(x => x.Pkey);
        Map(x => x.Name);
        HasManyToMany(x => x.SubBrokers).Table("Intermediary2SubBroker")
            .ParentKeyColumn("IntermediaryFKey")
            .ChildKeyColumn("SubBrokerFKey")
            .AsSet();
    }
 }
于 2013-01-01T15:37:56.687 回答
0

我认为您需要将 Friends 声明为virtual

这就是 Inner Exception 消息告诉您的内容:

“方法 get_Friends 应该是‘公共/受保护的虚拟’或‘受保护的内部虚拟’”

于 2013-01-02T15:36:43.547 回答