代码之家  ›  专栏  ›  技术社区  ›  tchrikch

“引用”属性在客户端不可见

  •  0
  • tchrikch  · 技术社区  · 14 年前

    所以像往常一样,我对RIA服务+NHibernate有问题。问题是如何使实体属性 ,使用引用映射,在客户端可见。问题是当加载实体时 如果没有此字段并尝试保存它,则数据库中缺少的值将更新为空。这里是类架构:

     public class A
    {
                public virtual ComplexProperty property {get;set;}
    }
    
    public class AMap
    {
      public  AMAP()
      {
       References(p=>p.property).Nullable().Column(“COMPLEX_PROPERTY_ID”);
      }
    }
    

    (我跳过了映射/声明键属性的部分,因为它是在底层类中生成的) 使用include和association属性(如hasmany)的常规技巧不起作用,因为内部没有真正的外键属性 A级

    1 回复  |  直到 14 年前
        1
  •  0
  •   tchrikch    14 年前

    找到一个适合我的解决方案。将“假”外键属性添加到未映射到数据库的类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;
    }