Readonly navigational property for collection

Thread is closed for posting
2 posts, 0 answers
  1. Arun
    Arun avatar
    2 posts
    Member since:
    Aug 2012

    Posted 25 Jun 2012 Link to this post

    Dear Support,

    Please help us resolve this below scenario.  

    we have 2 tables Order and OrderItems which is in One to Many relationship

    lets say Order table is PO10000 and Order Item table is PO10001, then the navigation property to PO10001 will be generated like below and it is read-only, Even if we add Set for this property, it will get overwrite whenever the model changes. Can we have a permanent solution for this. 

    private IList<PO10001> _pO10001 = new List<PO10001>();
    public virtual IList<PO10001> PO10001
            return this._pO10001;

    The basic idea to make this read-only is when we query PO10000 table, PO10001 should be fetched based on a filter condition and Order-by. Please find the query below.

    Order = (from c in dbContext.PO10000
                      where c.PRCHO_NUMBER.ToUpper() == pRchoNumber.ToUpper() && c.ARCHV_FLAG == char.Parse("N")
                       select c).FirstOrDefault();
    Order.PO10001 = purchaseOrder.PO10001.OrderBy(p => p.PO10001_ID).ToList();
    Order.PO10001 = purchaseOrder.PO10001.Where(p => p.ARCHV_FLAG == char.Parse("N")).ToList();
     return purchaseOrder;

    We would appreciate if you can even provide query that resolves this problem.


  2. Alexander
    Alexander avatar
    727 posts

    Posted 27 Jun 2012 Link to this post

    Hi Arun,

    I will post here the answer to your support ticket on the same topic, in case someone else hits the same problem:

    It is not possible to extend the read-only navigation property to become read-write.
    What you can do is to define a partial extension class for the PO10000 entity type and define there a new read-only property that filters the PO10001 items on demand:
    public  partial class PO10000
        public IQueryable<PO10001> FilteredPO10001
            get { return this.PO10001.Where(p => p.ARCHV_FLAG == char.Parse("N")).OrderBy(p => p.PO10001_ID); }

    Putting this code in a partial class in another code file will protect it from being overwritten when your data model changes. Also this implementation will guarantee that you always get the latest data from the database on each call of the FilteredPO10001 property.

    the Telerik team
    OpenAccess ORM Q2'12 Now Available! Get your hands on all the new stuff.
Back to Top