Error duplicate Key; already exists in the cache of the object scope

4 posts, 0 answers
  1. Harm
    Harm avatar
    2 posts
    Member since:
    Nov 2012

    Posted 24 Feb 2014 Link to this post

    hello together,

    I'm testing telerik and have the following problem.
    I want to insert all missing records from table "order" in table "orderXML".

    Table order
    BetrNr Char(6), (primary key)
    BelegNr int, (primary key)
    Lieferant int,
    .....

    Table order XML
    BetrNr Char(6), (primary key)
    BelegNr int, (primary key)
    uebertrage int ,
    erledigt int ,
    .....

    I can identify all the missing records and also insert:

              IQueryable<BESTELLKOPF> query = from b in dbContext.orders
                                                .Include(hms => hms.orderXML)
                                                join r in dbContext.orders
                                                on b.BelegNr equals r.BELEGNR into temp
                                                from t in temp.DefaultIfEmpty()
                                                where b.BetrNr == "100000" &&
                                                      b.Lieferant == 69 &&
                                                      (t.BETRNR == null ||
                                                        t.ISTERLEDIGT == 0)
                                                select b;
     
                // jetzt alle nicht enthaltenen Sätze in orderXML einfügen
                foreach (order bestellung in query)
                {
                    if (bestellung.orderXML == null)
                    {
                        orderXML hms = new HMS_BESTELLXML();
                        hms.BETRNR = bestellung.BetrNr;
                        hms.BELEGNR = bestellung.BelegNr;
                        hms.ISTERLEDIGT = 0;
                        hms.ISTUEBERTRAGEN = 0;
                        hms.BEMERKUNG = "";
                        brz.Add(hms);
                    }
                }
                dbContext.SaveChanges();

    this works fine.

    But if another program deletes records from the table "orderXML" the following error occurs:
    Error duplicate Key; already exists in the cache of the object scope

    I have tried the following

    dbContext.cache.releaseAll();

    dbContext.cache.release(dbContext.orderXMLs);


    set the property "cache Policy" of orderXML to "NoCache"



    Kind regards
    Harm




  2. Ralph Waldenmaier
    Admin
    Ralph Waldenmaier avatar
    202 posts

    Posted 24 Feb 2014 Link to this post

    Hello Harm,

    Thank you for your interest in Telerik DataAccess.

    I think the problem in your case is the query and the respective joins. Can you please try to see if the following query fulfills your requirements?

    IQueryable<BESTELLKOPF> query = from b in dbContext.orders                                            
                                    where
                                    b.orderXML == null &&
                                    b.BetrNr == "100000" &&
                                    b.Lieferant == 69                              
                                    select b;

    In this case we are generating an outerjoin to the orderXml table and all the orders where the orderXml reference is null are returned.



    Can you try to see if you are still experience the duplicate object key problems?

    Hope this helps.

    Do come back in case you have any other question.



    Regards,

    Ralph Waldenmaier
    Telerik
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.

  3. DevCraft banner
  4. Harm
    Harm avatar
    2 posts
    Member since:
    Nov 2012

    Posted 24 Feb 2014 in reply to Ralph Waldenmaier Link to this post

    Hello Ralph,

    the query works, but I get no records.
    I want to get all records that are "istErledit == 0" or not exists in table orderXML.

    Your query gives me first back all the missing records. After inserting the records the query is empty.

    Kind Regards
    Harm
  5. Ralph Waldenmaier
    Admin
    Ralph Waldenmaier avatar
    202 posts

    Posted 26 Feb 2014 Link to this post

    Hello Harm,

    Have you tried to add the condition to the where clause of your query like this?

    IQueryable<BESTELLKOPF> query = from b in dbContext.orders                                            
                                    where
                                    (b.orderXML == null || b.ISTERLEDIGT == 0 )&&
                                    b.BetrNr == "100000" &&
                                    b.Lieferant == 69                              
                                    select b;


    This should give you back all the ones that are not yet in the orderXML table and also the ones that fulfil the ISTERLEDIGT == 0  condition.



    Hope this helps.

    Feel free to ask in case you have any other question.



    Regards,

    Ralph Waldenmaier
    Telerik
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.

Back to Top