tags:

views:

173

answers:

3

I am stuck with this problems for a long time now.

Everything I try to do is insert a row in my DB if it's new information - if not update the existing one.

I've updated many entities in my life before - but what's wrong with this code is beyond me (probably something pretty basic) I guess I can't see the wood for the trees...

private Models.databaseDataContext db = new Models.databaseDataContext();

internal void StoreInformations(IEnumerable<EntityType> iEnumerable)
{
 foreach (EntityType item in iEnumerable)
 {
  EntityType type = db.EntityType.Where(t => t.Room == item.Room).FirstOrDefault();
  if (type == null)
  {
   db.EntityType.InsertOnSubmit(item);
  }
  else
  {
   type.Date = item.Date;
   type.LastUpdate = DateTime.Now();
   type.End = item.End;
  }       
 }
}

internal void Save()
{
 db.SubmitChanges();
}

Edit: just checked the ChangeSet, there are no updates only inserts. For now I've settled with

foreach (EntityType item in iEnumerable)
{
    EntityType type = db.EntityType.Where(t => t.Room == item.Room).FirstOrDefault();
    if (type != null)
    {
        db.Exams.DeleteOnSubmit(type);
    }
    db.EntityType.InsertOnSubmit(item);
}

but I'd love to do updates and lose these unnecessary delete statements.

A: 

Wouldn't you want item.Room instead of iEnumerable.Room?

Aaron Daniels
true and I edited the question accordingly since it doesn't solve the underlying problem
Christina Mayers
A: 

I wonder if you are experiencing issues due to the problem discussed here: http://stackoverflow.com/questions/751776/why-cant-i-change-elements-from-a-linq-ienumerable-in-a-for-loop

One way to test it would be to pass in a List of EntityType instead of IEnumerable as such:

internal void StoreInformations(IList<EntityType> iEnumerable)
{
...
}
John Allers
Just tried, sadly it doesn't change anything (since I am not updating iEnumerable).I checked everythin else too, still no luck
Christina Mayers
I guess I may have dismissed your answer a bit too soon.Since nothing was new I only saw three inserts. But I tried to force an update - I get for each insert an update - so the original value will be updated and a new one inserted...
Christina Mayers
So have you made any progress or are you going to stick with the workaround you posted above?In my answer I was thinking that in the section you were doing the updating, `item` was being destroyed after the foreach loop and maybe that's why `type` wasn't being updated.Recently I've been bitten by issues with trying to do updates while working with IEnumerable<> and I resolved my issue by using IList<> in certain places.I'm new to Linq myself, but your issue looked somewhat similar, so I thought maybe using IList in place of IEnumerable might work. But I don't know enough to say why.
John Allers
sorry I didn't comment any sooner, I ditched LinqToSql entirely - it's dead technology anyway. :)
Christina Mayers
A: 

Well, I worked out why it didn't work the way I anticipated it.

function Stuff()
{    
    Wrapper x = new Wrapper();
    Type a = new Type();
    InsertType(a);
    x.Type = a;
    InsertOnSubmit(x);
}

function InsertType (Type a)
{
    InsertOnSubmit(a);
}

since a in x is still the same a will be inserted a second time - it's still new. Somehow I thought that they were referenced and essentially the same.

I haven't thought about why it does work when I delete the old record first. Don't want to waste any more time on this. On to new worlds (EF4 or NHibernate)!

Christina Mayers