This is a migrated thread and some comments may be shown as answers.

Readonly navigational property for collection

1 Answer 60 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Arun
Top achievements
Rank 1
Arun asked on 25 Jun 2012, 10:46 AM
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
{
    get
    {
        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.

Thanks,
Arun 

1 Answer, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 27 Jun 2012, 01:57 PM
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.

Regards,
Alexander
the Telerik team
OpenAccess ORM Q2'12 Now Available! Get your hands on all the new stuff.
Tags
LINQ (LINQ specific questions)
Asked by
Arun
Top achievements
Rank 1
Answers by
Alexander
Telerik team
Share this question
or