简体   繁体   English

流利的Nhibernate映射帮助

[英]Fluent Nhibernate mapping help

Domain: 域:

 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; }
}

Mapping: 对应:

 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");
    }

Issue: 问题:

When saving a new person with an account id everything saves OK except for the acccount id field. 用帐户ID保存一个新人时,除“帐户ID”字段外,其他所有内容都可以保存。

A person doesn't need to have an account to exist. 一个人不需要拥有一个帐户就可以存在。

What am I doing wrong? 我究竟做错了什么?

Thanks. 谢谢。

HNibernate doesn't know what to do with your AccountId. HNibernate不知道如何处理您的AccountId。 On your person object, you've got an Account and an AccountId property. 在您的人员对象上,您有一个Account和一个AccountId属性。 I'll bet if you assigned the Account to the person before you save it, it'd all start working. 我敢打赌,如果您在保存之前将帐户分配给了该人,它将全部开始工作。

Get rid of that AccountId property. 摆脱该AccountId属性。 You don't need it. 不用了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM