【发布时间】:2014-11-18 16:09:23
【问题描述】:
我是 NHibernate 的新手。我在 sql server 中创建了两个表,例如 UserDetails 及其对应的 Products 表。映射就像! 我的实体类
公共类 UserDetails {
[Required(ErrorMessage = "Please enter User Id")]
public virtual string UserId { get; set; }
[Required(ErrorMessage = "Please enter User Name")]
public virtual string UserName { get; set; }
[Required(ErrorMessage = "Please enter Password")]
public virtual string Password { get; set; }
[Required(ErrorMessage = "Please enter Email Id")]
[EmailAddress(ErrorMessage = "Please enter proper Email Id")]
public virtual string EmailId { get; set; }
[Required(ErrorMessage = "Please enter Address")]
[DataType(DataType.MultilineText)]
public virtual string Address { get; set; }
[Required(ErrorMessage = "Please enter Phone No")]
public virtual string PhoneNo { get; set; }
public virtual ISet<Product> Products { get; set; }
}
公开课产品 {
[HiddenInput(DisplayValue=false)]
public virtual int ProductID { get; set; }
[Required(ErrorMessage="Please enter a product name")]
public virtual string Name { get; set; }
[DataType(DataType.MultilineText)]
[Required(ErrorMessage = "Please enter a description")]
public virtual string Description { get; set; }
[Required]
[Range(0.01,double.MaxValue,ErrorMessage="Please enter a positive price")]
public virtual decimal Price { get; set; }
[Required(ErrorMessage = "Please specify a category")]
public virtual string Category { get; set; }
public virtual byte[] ImageData { get; set; }
[HiddenInput(DisplayValue=false)]
public virtual string ImageMimeType { get; set; }
public virtual UserDetails UserDetail { get; set; }
}
映射 UserDetails.hbm.xml
<class name="SportsStore.Domain.Entities.UserDetails,SportsStore.Domain" table="UserDetails" lazy="true" >
<id name="UserId" column="UserId" access="property" type="String" >
<generator class="assigned"></generator>
</id>
<property name="UserName" column="UserName" access="property" type="String" ></property>
<property name="Password" column="Password" access="property" type="String"></property>
<property name="EmailId" column="EmailId" access="property" type="String" ></property>
<property name="Address" column="Address" access="property" type="String" ></property>
<property name="PhoneNo" column="PhoneNo" access="property" type="String" ></property>
<set name="Products" lazy="true" cascade="all-delete-orphan" inverse="true" >
<key column="UserId" ></key>
<one-to-many class="SportsStore.Domain.Entities.Product"></one-to-many>
</set>
Products.hbm.xml
<class name="SportsStore.Domain.Entities.Product,SportsStore.Domain" table="Products">
<id name="ProductID" column="ProductID" access="property" type="Int32" >
<generator class="native"></generator>
</id>
<property name="Name" column="Name" access="property" type="String">
</property>
<property name="Description" column="Description" access="property" type="String"></property>
<property name="Price" column="Price" access="property" type="decimal"></property>
<property name="Category" column="Category" access="property" type="String"></property>
<property name="ImageData" column="ImageData" access="property" type="BinaryBlob" length="2147483647"></property>
<property name ="ImageMimeType" column="ImageMimeType" access="property" type="String" ></property>
<many-to-one name="UserDetail" column="UserId" class="SportsStore.Domain.Entities.UserDetails" access="property" cascade="all"></many-to-one>
var user = (from userDetails in _session.Query<UserDetails>()
where userDetails.UserId == userId && userDetails.Password == password
select userDetails);
我以为我只会从 userdetails 获取数据,因为它应用了延迟加载,但在调试模式下我也可以从 products 表中看到数据。为什么?我是否以错误的方式理解延迟加载,或者我的映射中是否有任何错误?据我所知,延迟加载仅按需加载数据意味着如果我们进行迭代,则数据将从产品表中加载。
【问题讨论】:
-
通过访问调试器中的属性,NHibernate 可能会为您从数据库中获取信息。你应该附上一个分析器来看看到底发生了什么