NHibernate使用代码映射实现一对一映射
我的班级结构如下:NHibernate使用代码映射实现一对一映射,nhibernate,mapping-by-code,Nhibernate,Mapping By Code,我的班级结构如下: public class BaseEntity { public Guid Id { get; set; } } // CREATE TABLE Project (Id, Name) public class Project : BaseEntity { public ProjectProperties Properties { get; set; } public string Name { get; set; } } // CREATE TAB
public class BaseEntity
{
public Guid Id { get; set; }
}
// CREATE TABLE Project (Id, Name)
public class Project : BaseEntity
{
public ProjectProperties Properties { get; set; }
public string Name { get; set; }
}
// CREATE TABLE ProjectProperties (Id, Markup)
// ForeignKey from ProjectProperties.Id -> Project.Id
public class ProjectProperties : BaseEntity
{
public int Markup { get; set; }
}
使用NH3.2和代码映射的正确方法是什么?我找不到通过PKs实现1:1关系的示例。您可以使用Join,因为主键匹配。它甚至不需要自己的Id,因为它依赖于项目
public class ProjectProperties
{
public int Markup { get; set; }
}
// in ProjectMapping
Join("ProjectProperties", join =>
{
join.Key("Id");
join.Component(x => x.ProjectProperties, c =>
{
c.Property(x => x.Markup);
}
});
我认为你应该使用这个代码
OneToOne(x => x.Properties,
x => x.PropertyReference(typeof(ProjectProperties).GetProperty("Properties")));