NHibernate流畅映射到实体属性

时间:2015-06-06 16:25:56

标签: asp.net-mvc-4 nhibernate fluent-nhibernate nhibernate-mapping fluent-nhibernate-mapping

我有一个名为“Customers”的表,在此表中有商店和办公室地址。在代码中有一个Customer类,其中包含两个类型为StoreAddress的属性(一个用于OfficeAddresspublic 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);

我可以映射不属于实体类型地址但不确定如何映射到客户实体中的其他实体属性的项目吗?..

StoreAddress

如何从表Customers表中映射到我的OfficeAddress public function update($tablename, $value, $where){ $Update = " UPDATE ".$tablename." SET "; $array_keys = array_keys($value); $array_values= array_values($value); $count = count($array_keys); for($i=0; $i< $count; $i++){ $value[$i] = " = '".$value[$array_keys[$i]]."' "; } //$value = implode(" ",$value); print_r($value); $Update .= " ".$value." WHERE ".$where; $Utest = $this->DbCon->query($Update); if($Utest){ return true; }else{ return false; }

2 个答案:

答案 0 :(得分:0)

如果我理解你的遗漏点,我们可以使用:

References / many-to-one

  

参考是用于在两个实体之间创建多对一关系,并且应用于&#34;许多方面。&#34;您正在引用单个其他实体,因此您使用References方法。 #HasMany / one-to-many是&#34;另一方&#34;参考关系的关系,并应用于&#34;一方。&#34;

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提供) - 这是关于代码映射,但提供了与流利的语法(帖子的后半部分)

答案 1 :(得分:0)

您可以使用component mapping

Component(x => StoreAddress).ColumnPrefix("StoreAddress");
Component(x => OfficeAddress).ColumnPrefix("OfficeAddress");

然后为地址类型创建一个组件映射:

public class AddressMap : ComponentMap<Address>
{
    public AddressMap()
    {
        //map properterties
    }
}