NHibernate 流利映射到实体 属性
NHibernate fluent mapping to an entity property
我有一个名为 "Customers" 的 table,在这个 table 中有商店和办公室地址。在代码中有一个 Customer class,它有两个类型为 Address
的属性(一个用于 StoreAddress
和 OfficeAddress
)。
public class Customer
{
public virtual int Id { get; set;}
public virtual string Name {get;set;}
public virtual string Email {get; set;}
public virtual Address StoreAddress {get; set;}
public virtual Address OfficeAddress {get; set;}
}
public class Address
{
public string Address1 {get; set;}
public string Address2 {get; set;}
public string State {get; set;}
public string City {get; set;}
public string Zip {get; set;}
}
我可以映射不属于实体类型地址的项目,但不确定如何映射到客户实体中的另一个实体 属性?..
Table("Customers");
Schema("dbo);
Id(x => x.ID).Column("CustomerId");
Map(x => x.Name);
Map(x => x.Email);
我如何才能从 table 客户 table 映射到我的 StoreAddress
和 OfficeAddress
?
如果我理解你的遗漏点,我们可以使用:
References / many-to-one
References is for creating many-to-one relationships between two entities, and is applied on the "many side." You're referencing a single other entity, so you use the References method. #HasMany / one-to-many is the "other side" of the References relationship, and gets applied on the "one side."
Table("Customers");
Schema("dbo);
Id(x => x.ID).Column("CustomerId");
Map(x => x.Name);
Map(x => x.Email);
// this is the fluent way for many-to-one
References(x => x.StoreAddress);
References(x => x.OfficeAddress);
可在此处找到 References()
语法的完整概述:Mapping-by-Code - ManyToOne (作者 Adam Bar) - 这是关于按代码映射的, 但提供比较流畅的语法(post的后半部分)
您可以使用 component mapping:
Component(x => StoreAddress).ColumnPrefix("StoreAddress");
Component(x => OfficeAddress).ColumnPrefix("OfficeAddress");
然后为地址类型创建组件映射:
public class AddressMap : ComponentMap<Address>
{
public AddressMap()
{
//map properterties
}
}
我有一个名为 "Customers" 的 table,在这个 table 中有商店和办公室地址。在代码中有一个 Customer class,它有两个类型为 Address
的属性(一个用于 StoreAddress
和 OfficeAddress
)。
public class Customer
{
public virtual int Id { get; set;}
public virtual string Name {get;set;}
public virtual string Email {get; set;}
public virtual Address StoreAddress {get; set;}
public virtual Address OfficeAddress {get; set;}
}
public class Address
{
public string Address1 {get; set;}
public string Address2 {get; set;}
public string State {get; set;}
public string City {get; set;}
public string Zip {get; set;}
}
我可以映射不属于实体类型地址的项目,但不确定如何映射到客户实体中的另一个实体 属性?..
Table("Customers");
Schema("dbo);
Id(x => x.ID).Column("CustomerId");
Map(x => x.Name);
Map(x => x.Email);
我如何才能从 table 客户 table 映射到我的 StoreAddress
和 OfficeAddress
?
如果我理解你的遗漏点,我们可以使用:
References / many-to-one
References is for creating many-to-one relationships between two entities, and is applied on the "many side." You're referencing a single other entity, so you use the References method. #HasMany / one-to-many is the "other side" of the References relationship, and gets applied on the "one side."
Table("Customers");
Schema("dbo);
Id(x => x.ID).Column("CustomerId");
Map(x => x.Name);
Map(x => x.Email);
// this is the fluent way for many-to-one
References(x => x.StoreAddress);
References(x => x.OfficeAddress);
可在此处找到 References()
语法的完整概述:Mapping-by-Code - ManyToOne (作者 Adam Bar) - 这是关于按代码映射的, 但提供比较流畅的语法(post的后半部分)
您可以使用 component mapping:
Component(x => StoreAddress).ColumnPrefix("StoreAddress");
Component(x => OfficeAddress).ColumnPrefix("OfficeAddress");
然后为地址类型创建组件映射:
public class AddressMap : ComponentMap<Address>
{
public AddressMap()
{
//map properterties
}
}