流利的Nhibernate映射帮助

时间:2011-08-02 21:10:57

标签: c# fluent-nhibernate

域:

 public class Account
{

    public virtual int AccountId { get; set; }
    public virtual int UserId { get; set; }
    public virtual string HostName { get; set; }
    public virtual DateTime CreatedOn { get; set; }
    public virtual bool Deleted { get; set; }
}

public class Person
{
    public Person()
    {
        PersonRoles = new List<PersonRole>();
    }
    public virtual int PersonId { get; set; }
    public virtual Guid PersonGuid { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string Surname { get; set; }
    public virtual string Email { get; set; }
    public virtual string Password { get; set; }
    public virtual string SaltKey { get; set; }
    public virtual int PersonType { get; set; }
    public virtual DateTime CreatedOn { get; set; }
    public virtual bool Deleted { get; set; }
    public virtual bool Active { get; set; }
    public virtual int? AccountId { get; set; }

    public virtual ICollection<PersonRole> PersonRoles { get; private set; }
    public virtual Account Account { get; set; }
}

映射:

 public AccountMap()
    {
        Id(x => x.AccountId, "AccountId").Column("AccountId");
        Map(x => x.UserId);
        Map(x => x.HostName);
        Map(x => x.CreatedOn);
        Map(x => x.Deleted);
        Table("crm_accounts");
    }

  public PersonMap()
    {
        Id(x => x.PersonId).Column("PersonId");
        Map(x => x.PersonGuid);
        Map(x => x.FirstName);
        Map(x => x.Surname);
        Map(x => x.Email);
        Map(x => x.Password);
        Map(x => x.SaltKey);
        Map(x => x.PersonType);
        Map(x => x.CreatedOn);
        Map(x => x.Deleted);
        Map(x => x.Active);

        HasManyToMany<PersonRole>(x => x.PersonRoles)
            .ParentKeyColumn("RoleId")
            .ChildKeyColumn("PersonId")
            .Cascade.All()
            .Table("crm_people_roles_mapping");

        //Map(x => x.AccountId);
        References(x => x.Account, "AccountId").Column("AccountId");
        Table("crm_people");
    }

问题:

使用帐户ID保存新人时,除了帐户ID字段外,所有内容都会保存正常。

一个人不需要存在帐户。

我做错了什么?

感谢。

1 个答案:

答案 0 :(得分:1)

HNibernate不知道如何处理您的AccountId。在您的person对象上,您有一个Account和一个AccountId属性。我敢打赌,如果你在保存帐户之前将帐户分配给该人,它就会开始工作。

摆脱该AccountId属性。你不需要它。