Fluent-nhibernate HasOne 关系返回 NULL
我在 fluid-nhibernate 中表达我想要的结果时遇到一些困难。也许我只是对这样一个简单的概念采取了错误的方法。
People 实体由第三方服务填充。稍后可以生成一个帐户(凭据以及将用作 api 密钥的 GUID)。用户只有一组凭据,并且每个用户的凭据都是唯一的。
在 Web 应用程序管理区域中,需要列出 People 实体的一些属性及其用户名。
最终在查询所有人员实体时,Account
始终为 NULL。
- 这是 HasOne 的错误用法吗?
本质上我想要的是在帐户上执行左外连接。
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
)
我在这里做的事情完全错误吗?
I'm having some difficulties representing my desired outcome within fluent-nhibernate. Perhaps I'm simply taking the wrong approach on such a simple concept.
The People entity is populated from a 3rd party service. At a later date an account ( credentials along with a guid that will be used as an api key) can be generated. A user will only have one set of credentials and credentials are unique per user.
Within the web application admin area it is desired to list some properties of the People entity along with their UserName.
Ultimately when querying for all people entities, the Account
is always NULL.
- Is this the the incorrect usage of HasOne?
Essentially what I want is to basically perform a left outer join on 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();
}
}
When running my PersistenceSpecification tests the tables are generating as I would expect:
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
)
Am I doing something totally wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
似乎您需要在 People 和 Account ClassMap 实现中使用
HasOne
。您当前将帐户显示为References
People,但反之则不然。Seems like you need to use
HasOne
in both the People and Account ClassMap implementations. You currently show Account asReferences
People but not the other way around.事实证明,我的映射对于我想要完成的任务是正确的,但是打开了一个无状态会话,这最终是我问题的根源。
Well it turns out that my mappings were correct for what I had wanted to accomplish, however a stateless session had been opened which was ultimately the source of my issues.