我使用LINQ到实体(C#的WinForms)和我的数据库结构如下建议的方法: 在数据库回滚
所有的拳头,我在表中插入新记录“creditos”,因为所有的表需要知道这张表的PK。我用的方法与像这样
Credito cred = new Credito();
cred.Producto = credito.producto;
cred.Cantidad = credito.monto_prestamo;
cred.TasaInteres = credito.tasa_interes;
然后
context.creditos.AddObject(cred);
context.SaveChanges();
//Get the ID of the inserted record
credito.idCredito = cred.IDCredito;
使用所得到的表“creditos”的PK,我使用类似的方法中的其他表中插入此作为FK。所以这里的问题是:如果其中一个插入失败,我该如何进行回滚?假设我已经在两个表中插入了记录,但是它没有在第三个表中插入,我该如何删除所有更改?
如果所有实体正确映射(像你解释一下),我称之为单一的SaveChanges,但一些插入失败,将SaveChanges方法处理回滚操作? –
默认情况下,它会创建一个事务,这意味着它会在失败时回滚。检查更多的这样的问题:http://stackoverflow.com/questions/815586/entity-framework-using-transactions-or-savechangesfalse-and-acceptallchanges –
感谢的人,我今天新学到了一些东西! –