views:

40

answers:

1

So as usual I have an issue with ria service + nhibernate. The question is how to make an entity property , mapped using “references”, visible on the client side. The problem is that when you load an entity without this field and try to save it , then missing values are updated as NULL inside db. Here’s class schema:

 public class A
{
            public virtual ComplexProperty property {get;set;}
}

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

(I've skipped parts with mapping/declaring key property as it's made inside underlying classes) Usual trick with include and association attribute(like with HasMany) does not work as there is no real foreign_key property inside class A

A: 

found a solution that's working for me. It's enough to add "fake" foreign key property to class A which is not mapped to database. It allows to define association:

       public class A
       {
           [Include]
           [Association("Relation1","ComplexPropertyId","Id")]
           public virtual ComplexProperty property {get;set;}
           public virtual int ? ComplexPropertyId {get;set;}
       }

Last thing to do is to set ComplexPropertyId manually on the client side after retrieving objects from db(mappings remain as they were).

public IQueryable<A> GetA()
{
 var item = repository.Query<A>();
 foreach(var a in item) a.ComplexPropertyId = a.ComplexProperty.Id;
 return item;
}
tchrikch