簡體   English   中英

fluent-nhibernate HasOne關系返回NULL

[英]fluent-nhibernate HasOne relationship returning NULL

我在流利的nhibernate中表達了我想要的結果有些困難。 也許我只是在這么簡單的概念上采取錯誤的方法。

People實體由第三方服務填充。 在以后的日期,可以生成一個帳戶(憑證以及將用作API密鑰的guid)。 用戶只有一組憑據,憑證對每個用戶是唯一的。

在Web應用程序管理區域中,需要列出People實體的一些屬性及其UserName。

最終,在查詢所有人員實體時, Account始終為NULL。

  • 這是HasOne的錯誤用法嗎?

基本上我想要的是基本上在Account上執行左外連接。

public class Account
{
    public virtual Guid Id { get; set; }
    public virtual string UserName { get; set; }
    public virtual string Password { get; set; }

    public virtual People People { get; set; }
}

public class People
{
    public virtual int UserID { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }

    public virtual Account Account { get; set; }
    ...
    ...
    ...
}

 public class AccountMap: ClassMap<Account>
    {
        public AccountMap()
        {
            Table("Account");
            LazyLoad();
            Id(x => x.Id).GeneratedBy.Assigned().Column("ID");
            Map(x => x.UserName).Column("UserName").Not.Nullable().Length(100);
            Map(x => x.Password).Column("Password").Not.Nullable().Length(100);
            References(x => x.People).Column("People_id");
        }
    }

public class PeopleMap : ClassMap<People>
{
    public PeopleMap()
    {
        Table("People");
        LazyLoad();
        Id(x => x.UserID).GeneratedBy.Identity().Column("People_id");
        Map(x => x.FirstName).Column("First_Name").Length(50);
        Map(x => x.LastName).Column("Last_Name").Length(50);
        HasOne(x => x.Account).PropertyRef(r => r.People).Cascade.All();
    }
}

在運行我的PersistenceSpecification測試時,表正在按照我的預期生成:

create table Account (
        ID UNIQUEIDENTIFIER not null,
       UserName TEXT not null,
       Password TEXT not null,
       People_id INT,
       primary key (ID),
       constraint FKBE1051AFE1BC1FAE foreign key (People_id) references People
    )

我在這里做錯了嗎?

似乎您需要在People和Account ClassMap實現中使用HasOne 您目前將帳戶顯示為References人員,但不是相反。

事實證明,我的映射對於我想要完成的事情是正確的,但無狀態會話已經打開,這最終是我的問題的根源。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM