[英]Mapping Fluent NHibernate where the primary key should also be a foreign key
I have a user object like so: 我有一个像这样的用户对象:
public class User
{
public Guid UserId { get; set; }
public string UserName { get; set; }
...
}
this is fluently mapped in nhibernate: 这是在nhibernate中流畅映射的:
public class UserMap : ClassMap<User>
{
public UserMap()
{
Id(x => x.UserId).Column("UserId");
Map(x => x.UserName).Not.Nullable();
...
}
}
I'm trying to build a credential block that is separate from the user object, so the password and salt are not carried around in the user class like so: 我正在尝试构建一个与用户对象分开的凭据块,因此密码和salt不会在用户类中携带,如下所示:
public class UserCredential
{
public User User { get; set; }
public byte[] Password { get; set; }
public string Salt { get; set; }
}
...but I cannot figure out how to properly map this. ...但我无法弄清楚如何正确映射这个。 Ultimately, in the database, I would expect to see a
UserId
column in the UserCredentials
table, that is both a primary key and a foreign key to the Users
table. 最终,在数据库中,我希望在
UserCredentials
表中看到UserId
列, UserCredentials
是Users
表的主键,也是Users
表的外键。 The Users
table should have no reference to the UserCredentials
table. Users
表应该没有对UserCredentials
表的引用。 How would I write that ClassMap<UserCredential>
class? 我该如何编写
ClassMap<UserCredential>
类?
This seems to be a one-to-one
relationship and would therefore be mapped using HasOne
in FNH. 这似乎是
one-to-one
关系,因此将使用FNH中的HasOne
进行映射。
public UserCredentialMap()
{
Id(x => x.Id)
.Column("UserId")
.GeneratedBy.Foreign("User");
HasOne(x => x.User).Constrained();
}
You may be able to map it like this as well: 您也可以像这样映射它:
public UserCredentialMap()
{
Id(x => x.Id, "UserId");
References(x => x.User, "UserId")
.Not.Update()
.Not.Insert();
}
As you don't want to give the Credentials
it's own primary key, it seems to me that what you really want is a Component ( more details ). 因为你不想给
Credentials
它自己的主键,在我看来你真正想要的是一个组件 ( 更多细节 )。
This will mean that the Credential
data is stored in the same table as the User
data, but is modelled in your domain as a separate object which is a property of your User class. 这意味着
Credential
数据与User
数据存储在同一个表中,但在您的域中将其建模为单独的对象,这是User类的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.