はこのような何か試してみてください(擬似コードを、私はmisrememberedいくつかのメソッド名かもしれません):このコードはStub Entityトリックを使用しています
public void Update(Customer entity)
{
using (MyContext ctx = new MyContext())
{
// Create a stub entity and attach it
Customer db = new Customer {ID = entity.ID};
ctx.Customers.Attach(db); // ctx.AttachTo("Customers", db) in 3.5 sp1
// ApplyPropertyChanges in 3.5 Sp1
ctx.ApplyCurrentValues(entity);
ctx.SaveChanges();
}
...
}
を。元のエンティティについてEFにもっと詳しく説明する必要がある場合は、上記のブログ記事を参照してください。スタブを使用することもできます。また
あなたはちょうどこの操作を行うことができ、並行性については全く気にしない場合:
public void Update(Customer entity)
{
using (MyContext ctx = new MyContext())
{
// pull the entity from the database
Customer db = ctx.Customers.First(c => c.ID == entity.ID);
// ApplyPropertyChanges in 3.5 Sp1
ctx.ApplyCurrentValues(entity);
ctx.SaveChanges();
}
}
希望これは
アレックス・ジェームス
Entity Framework Tips
'ApplyCurrentValuesを()'役立ちます方法は私のために存在しません。 –
@Alex James:Stub EntityとEntity Framework Tipsへのリンクをありがとう。それらは知っておくべき素晴らしいトリックです。 – muruge