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

Insert commands order

5 Answers 60 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Marko Gluhak
Top achievements
Rank 1
Marko Gluhak asked on 17 Feb 2012, 07:49 PM
Hi,

I'm not sute is it "problem" of OA or something else but here is my situation.
I have for loop in which I add some persistant classes do context with dbCiontext.Add(class) Wehn I'm done adding then I call SaveChanges().

What happens is that in database rows are not inserted in a way I add them to dbContext. I try change workflow and add savechanges() after every dbContext.Add(class) call. Then everything is OK and data are stored as they should be.

Table in which I store data has AutoInc primary key (if that's relevant).

Marko

5 Answers, 1 is accepted

Sort by
0
PetarP
Telerik team
answered on 22 Feb 2012, 05:38 PM
Hello Marko Gluhak ,

 Do you have any associations defined in any of the entities that you are adding to your context. It is possible that any pending association resolutions might change the order of the entities during commit.
Can you also please try to add all your entities to a collection first and then pass this collection to the Add method.

Regards,
Petar
the Telerik team
Telerik OpenAccess ORM Q1 2012 release is here! Check out what's new or download a free trial >>
0
Marko Gluhak
Top achievements
Rank 1
answered on 02 Mar 2012, 12:09 PM
Hi,

its definitly wrong. I try to add objects to list and then add taht list to dbContext as you sudgested but it did not help. I try it another project and the same thing happen. This is situation in another project.

I have this text:
45 Građevinarstvo
52.72 Popravak električnih aparata za kućanstvo
63.30 Djelatnost putničkih agencija i turoperatora
71.22 Iznajmljivanje plovila
72 Računalne i srodne aktivnosti
74.30 Tehničko ispitivanje i analiza

Number is one filed and text is another. Its important to have in this particular order. Every objet is connected to another one with relation ship. This is code that I use to add those obect to dbContext.
foreach ( var djelatnost in djelatnosti )
{
    var  podaci = djelatnost.SelectNodes( "td" );
 
    if ( podaci[0].InnerText != "*" )
    {
        var activity = new cFirmActivity( );
        activity.CFirm = firm;
        activity.Number = podaci[0].InnerText;
        activity.Activity = podaci[1].InnerText;
 
        list.Add( activity );
        //_db.SaveChanges();
    }
}
 
if ( list.Count > 0 )
{
    _db.Add( list );
}

One more note is that firm is already added to dbContext but not in db (but same thigs happen if I save firm first).

I have debug app and set breakpoint to _db.Add(list);
all aobjects are as they need to be. After I save _db.SaveChanges() this is what I get in DB.

IdFirmAc    IdFirm  Number  Activity
51          21      74.30   Tehničko ispitivanje i analiza
52          21      72      Računalne i srodne aktivnosti
53          21      71.22   Iznajmljivanje plovila
54          21      45      Građevinarstvo
55          21      52.72   Popravak električnih aparata za kućanstvo
56          21      63.30   Djelatnost putničkih agencija i turoperatora

You can see that Filed "Number" is not sorted as they are in text and also in list variable.

In order to make it right I need to call SaveChanges on every Add method. Here is code that works ok:

foreach ( var djelatnost in djelatnosti )
{
    var  podaci = djelatnost.SelectNodes( "td" );
 
    if ( podaci[0].InnerText != "*" )
    {
        var activity = new cFirmActivity( );
        activity.CFirm = firm;
        activity.Number = podaci[0].InnerText;
        activity.Activity = podaci[1].InnerText;
 
        _db.Add( activity );
        _db.SaveChanges();
    }
}

0
PetarP
Telerik team
answered on 08 Mar 2012, 04:33 PM
Hello Marko Gluhak,

 Unfortunately if there aren't any constraints in regards to your objects OpenAccess cannot really guarantee of the order in which the entities will be inserted. Having said that in order to preserve the order you want the only possibility would be to call save changes after each insert.

Kind regards,
Petar
the Telerik team
Telerik OpenAccess ORM Q1 2012 release is here! Check out what's new or download a free trial >>
0
Marko Gluhak
Top achievements
Rank 1
answered on 08 Mar 2012, 04:53 PM
Hi,

in my reply I have written the way that it would work and it was not my question. My question was is this behaviour expected or not (for me its not, but maybe there are some other contraints that you need to take care are effecting this).

I think that everyone will agree that its expected from OpenAccess to save rows in a way that they put them in context.
Thank you.
0
Accepted
PetarP
Telerik team
answered on 13 Mar 2012, 12:39 PM
Hi Marko Gluhak,

 As I mentioned in my previous reply - unless you have some pending constraints that force the entities to be inserted in a given order, OpenAccess does not guarantee that the insert will happen in the way you have added the objects to the context.
The only solution for that is to save after each entity adding. This feature is not part of the product because in theory the order in which your entities have been added to the database should not matter at all. Please share with us how important is that for your product and perhaps why? Maybe we can suggest you a better way for your application.

Greetings,
Petar
the Telerik team
Telerik OpenAccess ORM Q1 2012 release is here! Check out what's new or download a free trial >>
Tags
General Discussions
Asked by
Marko Gluhak
Top achievements
Rank 1
Answers by
PetarP
Telerik team
Marko Gluhak
Top achievements
Rank 1
Share this question
or