Fluent Nhibernate 映射帮助

发布于 2024-11-27 17:26:06 字数 2293 浏览 1 评论 0原文

域:

 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 字段外,所有内容均保存正常。

一个人不需要拥有帐户即可存在。

我做错了什么?

谢谢。

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.

A person doesn't need to have an account to exist.

What am I doing wrong?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

も星光 2024-12-04 17:26:06

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

删除该 AccountId 属性。你不需要它。

HNibernate doesn't know what to do with your AccountId. On your person object, you've got an Account and an AccountId property. 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. You don't need it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文