“References”属性在客户端不可见
问题描述:
所以像往常一样,我有一个与ria服务+ nhibernate的问题。问题是如何使用“引用”映射的实体属性 在客户端可见。问题是当你加载一个没有这个字段的实体 并且尝试保存它时,那么缺失值在db内更新为NULL。这里的类架构:“References”属性在客户端不可见
public class A
{
public virtual ComplexProperty property {get;set;}
}
public class AMap
{
public AMAP()
{
References(p=>p.property).Nullable().Column(“COMPLEX_PROPERTY_ID”);
}
}
惯用的伎俩与包括和关联属性(如使用的hasMany)不起作用,因为没有(我已经跳过部分与映射/因为它的基础类中作出声明键属性)真正的foreign_key属性 A级
答
找到了一个适用于我的解决方案。将“假”外键属性添加到未映射到数据库的类A足够了。它允许定义关联关系:
public class A
{
[Include]
[Association("Relation1","ComplexPropertyId","Id")]
public virtual ComplexProperty property {get;set;}
public virtual int ? ComplexPropertyId {get;set;}
}
最后一件事做的是从数据库中检索对象后手动设置ComplexPropertyId
在客户端(映射都保持着)。
public IQueryable<A> GetA()
{
var item = repository.Query<A>();
foreach(var a in item) a.ComplexPropertyId = a.ComplexProperty.Id;
return item;
}