[英]Mapping by code Zero-Or-One relationship
I have the following database structure, which I cannot change : 我有以下数据库结构, 我无法更改 :
CREATE TABLE Users (
ID INT NOT NULL IDENTITY(1,1),
PRIMARY KEY(ID)
)
CREATE TABLE UserAvatars (
UserID INT NOT NULL,
Width INT NOT NULL,
Height INT NOT NULL,
PRIMARY KEY(UserID),
FOREIGN KEY(UserID) REFERENCES Users(ID),
)
Basically, a User can have Zero-or-one avatars (I removed columns to simplify the example). 基本上,用户可以拥有零或一个头像(我删除了列以简化示例)。
class User
{
public int ID { get; protected set; }
public UserAvatar Avatar { get; set; }
}
class UserAvatar
{
public User User { get; set; }
public int Width { get; set; }
public int Height { get; set; }
}
I am using the following mapping: 我使用以下映射:
class UserMapping : ClassMapping<User>
{
public UserMapping()
{
Id(x => x.ID, m => {
m.Generator(Generators.Identity);
});
OneToOne(x => x.Avatar);
}
}
class UserAvatarMapping : ClassMapping<UserAvatar>
{
public UserAvatar()
{
Id(x => x.User, m =>
{
m.Generator(Generators.Foreign<User>(u => u.ID));
});
Property(x => x.Width);
Property(x => x.Height);
}
}
However, when trying run my app, I get an NHibernate.MappingException
: 但是,在尝试运行我的应用程序时,我得到一个NHibernate.MappingException
:
NHibernate.MappingException : Could not compile the mapping document: mapping_by_code----> NHibernate.MappingException : Could not determine type for: MyApp.Models.UserAvatar, MyApp, for columns: NHibernate.Mapping.Column(User) NHibernate.MappingException:无法编译映射文档:mapping_by_code ----> NHibernate.MappingException:无法确定类型:MyApp.Models.UserAvatar,MyApp,用于列:NHibernate.Mapping.Column(User)
I can't make any sense of this cryptic error. 我无法理解这个神秘的错误。
How do I accomplish the mapping where a User
could have zero or one Avatars? 如何完成User
可以拥有零个或一个头像的映射?
Taking from Radim Köhler's guidence, the final classes / mapping that solved the problem: 根据RadimKöhler的指导,解决问题的最终类/映射:
class User
{
public int ID { get; protected set; }
public UserAvatar Avatar { get; set; }
}
class UserAvatar
{
public int UserID { get; protected set; }
public User User { get; set; }
public int Width { get; set; }
public int Height { get; set; }
}
With the following mapping: 使用以下映射:
class UserMapping : ClassMapping<User>
{
public UserMapping()
{
Id(x => x.ID, m => {
m.Generator(Generators.Identity);
});
OneToOne(x => x.Avatar, m =>
{
m.Cascade(Cascade.All);
m.Constrained(false);
});
}
}
class UserAvatarMapping : ClassMapping<UserAvatar>
{
public UserAvatar()
{
Id(x => x.UserID, m =>
{
m.Generator(Generators.Foreign<UserAvatar>(u => u.User));
});
OneToOne(x => x.User, m =>
{
m.Constrained(true);
});
Property(x => x.Width);
Property(x => x.Height);
}
}
The ID is important almost for every entity mapped by NHibernate (ORM). 对于NHibernate(ORM)映射的每个实体,ID几乎都很重要。 So, we should extend the Avatar class: 所以,我们应该扩展Avatar类:
class UserAvatar
{
// every(stuff) should have its ID
public int ID { get; protected set; }
public User User { get; set; }
...
The ID value will be managed by one-to-one relationship. ID值将通过一对一的关系进行管理。 So, now let's adjust the mapping. 所以,现在让我们调整映射。
public UserAvatar()
{
Id(x => x.ID);
HasOne(x => x.User)
.Constrained()
.ForeignKey();
...
public UserMapping()
{
Id(x => x.ID);
HasOne(s => s.Avatar).Cascade.All();
Related and really interesting reading: 相关且非常有趣的阅读:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.