0
 private void SuaKH()
    {
        KhachHang kh = new KhachHang();
        kh.MaKH = textBox1.Text;
        kh.TenKH = textBox2.Text;
        kh.SDT = textBox4.Text;
        kh.DiaChi = textBox3.Text;
        db.KhachHangs.Attach(kh);
        db.Entry(kh).State = EntityState.Modified;
        db.SaveChanges();
    }

Attaching an entity of type 'WindowsFormsApplication1.Models.KhachHang' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the

  • can you show your model? – Dylan Slabbinck Dec 02 '15 at 13:08
  • Please, convert this into a concrete question, or delete it. Thank you. It's not that hard: try something like "this is what I have, this is what I do, this is the error I get" By the way, the title is also awful – JotaBe Dec 02 '15 at 16:25
  • thanks everyone. I thought I had control over it. `` – Lê Lộc Dec 03 '15 at 11:28
  • ` using (Model db = new Model()) {} – Lê Lộc Dec 03 '15 at 11:30
  • You might have a look at my answer on [ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value](http://stackoverflow.com/questions/23201907/asp-net-mvc-attaching-an-entity-of-type-modelname-failed-because-another-ent/39557606#39557606). – Murat Yıldız Sep 18 '16 at 12:29

0 Answers0