|
楼主 |
发表于 2011-5-26 09:04:32
|
显示全部楼层
DariaK
3 Mar, 2008
Here is the tool for database synchronization that supports Linq to Sql object model:
http://perpetuumsoft.com/Product.aspx?lang=en&pid=55
I suppose It is worth to try!
Bruno Kenj
1 May, 2008
Pessoa p2 = new Pessoa();
p2.Id = 1;
p2.Nome = “Teste” + DateTime.Now.ToString();
DbContext.Pessoas.Attach(p2);
DbContext.Refresh(RefreshMode.KeepCurrentValues, p2);
DbContext.SubmitChanges();
this way, only chances are updated.
Richard Bushnell
1 May, 2008
@Bruno:
That looks interesting, but doesn’t it really do the same as doing a select before an update?
Richard Bushnell
29 May, 2008
Hi Neal,
Changeset.Dump() is something that only works in LinqPad.
– The changeset is generated from the DataContext, but in LinqPad, you don’t reference the DataContext, because you are actually inside it.
- Dump() is a LinqPad method defined as an extension method on Object. It is LinqPad specific.
If you want more information about LinqPad, you can contact the authors at http://www.linqpad.net. When I contacted them, they were very helpful. I’m sure they’d let you know how it works.
HTH,
Richard
Christian Zangl
1 Aug, 2008
If you go to your database.dbml and change the “Update Check” property on the Product fields to “Never” then your code will work and you’ll get the SQL you wanted:
var product = new Product();
product.ProductID = 1;
Products.Attach(product);
product.Name = “Richard’s product”;
SubmitChanges();
Martin Montgomery
28 Oct, 2008
Just a quick piece of code to do a general update on all records
private Product UpdateSomething(Product product)
{
product.value = 99;
return product;
}
public void UpdateAll()
{
var list = (from product in DataContext.GetTable()
select UpdateSomething(product)).ToList();
DataContext.SubmitChanges();
}
We can also include a where clause to filter those products being updated
Param Iyer
23 Nov, 2008
I could not find a solution to this problem for almost 5 hours before stumbling down here and writing the below piece of code…
Pessoa p2 = new Pessoa();
p2.Id = 1;
p2.Nome = “Teste” + DateTime.Now.ToString();
DbContext.Pessoas.Attach(p2);
DbContext.Refresh(RefreshMode.KeepCurrentValues, p2);
DbContext.SubmitChanges();
this thing does WORK
Damian
13 Jan, 2009
Pessoa p2 = new Pessoa();
p2.Id = 1;
p2.Nome = “Teste” + DateTime.Now.ToString();
DbContext.Pessoas.Attach(p2);
DbContext.Refresh(RefreshMode.KeepCurrentValues, p2);
DbContext.SubmitChanges();
It’s works.
Drako
31 Mar, 2009
This is my class DBMaintenance – all works fine
public object Insert(object item)
{
using (TransactionScope ts = new TransactionScope())
{
ITable itbl = DataContext.GetTable(item.GetType());
itbl.InsertOnSubmit(item);
itbl.Context.SubmitChanges();
ts.Complete();
}
return item;
}
public object Delete(object item)
{
using (TransactionScope ts = new TransactionScope())
{
ITable itbl = DataContext.GetTable(item.GetType());
itbl.DeleteOnSubmit(item);
itbl.Context.SubmitChanges();
ts.Complete();
}
return item;
}
public object Update(object item)
{
using (TransactionScope ts = new TransactionScope())
{
ITable itbl = DataContext.GetTable(item.GetType());
itbl.Attach(item);
itbl.Context.Refresh(RefreshMode.KeepCurrentValues, item);
itbl.Context.SubmitChanges();
ts.Complete();
}
return item;
}
Pure Krome
26 Oct, 2009
Folks, Christian Zangl post above is the correct answer IMO.
By default, Linq to Sql uses Optimistic Concurrency (aka. O.C.). Everyone knows that and we’re all not disputing that.
So _BY DEFINITION_ this means that we _cannot_ update our object UNLESS we know it’s the same version in the database – otherwise we need to throw an exception. That’s what O.C. is all about.
So -> Linq to Sql does this in two ways.
1) If you have a TIMESTAMP field in the table, then send that across the wire along with the Primary Key and make sure that the current object’s TIMESTAMP field (that is about to be updated) is the same as the DB’s.
-or-
2) No timestamp? then we need to check -each- field (which is what is happening in Dinesh’s example code, above).
So … maybe you don’t want O.C. then?! If you _want_ to make sure that the data you’re about to update IS not more recent that the version in the code, then yes .. u need O.C. Otherwise, don’t do Optimistic Concurrency and therefore all these hacks to get around it.
To not use O.C., make sure each field in the table that is being updated, the ‘Update Status’ field is set to NEVER. The default is Always … so switch it over to NEVER.
As is said, Christian Zangl has said it perfectly right, above.
Good Luck!
Pure Krome
26 Oct, 2009
Oops -> i have an error in the following sentence above.
“which is what is happening in Dinesh’s example code, above).”
should read as
“which is what is happening in Richard Bushnell’s example code, above).”
|
|