LinqToSql: insert instead of update
Posted
by Christina Mayers
on Stack Overflow
See other posts from Stack Overflow
or by Christina Mayers
Published on 2010-05-23T19:48:57Z
Indexed on
2010/05/23
19:50 UTC
Read the original article
Hit count: 305
c#
|linq-to-sql
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 == iEnumerable.Room).FirstOrDefault();
if (type == null)
{
db.EntityType.InsertOnSubmit(item);
}
else
{
cur.Date = item.Date;
cur.LastUpdate = DateTime.Now();
cur.End = item.End;
}
}
}
internal void Save()
{
db.SubmitChanges();
}
© Stack Overflow or respective owner