如何在实体框架中使用交易? [英] How to use Transaction in Entity Framework?

查看:74
本文介绍了如何在实体框架中使用交易?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在实体框架中使用交易?我在Stackoverflow上阅读了一些链接: http://stackoverflow.com/questions / 815586 / entity-framework-using-transactions-or -changechangesfalse-and-acceptallchanges [ ^ ]

但我有3个表,这意味着我有3个实体:



 CREATE TABLE Personel 
(PersonelID整数PRIMARY KEY身份不为空,
Ad varchar(30),
Soyad varchar (30),
Meslek varchar(100),
DogumTarihi datetime,
DogumYeri nvarchar(100),$ b $bPirimToplamıfloat);
Go

create TABLE Prim
(PrimID integer PRIMARY KEY identity not null,
PersonelID integer外键引用Personel(PersonelID),
SatisTutari int,
Prim float,
SatisTarihi Datetime);
Go

CREATE TABLE Finans
(ID整数PRIMARY KEY身份不为空,
Tutar float);





Personel,Prim,Finans是我的桌子。如果查看Prim表,可以看到Prim为浮点值。如果我写的不是浮点值,那么我的交易必须运行。

使用(TestEntities testCtx = new TestEntities())
{
using(TransactionScope scope) = new TransactionScope())
{
// do someyihng ...
testCtx.Personel.SaveChanges();
// do someyihng ...
testCtx.Prim.SaveChanges();
// do someyihng ...
testCtx.Finans.SaveChanges();
范围.Complete();
success = true;

}
}



我该怎么办?

解决方案

< blockquote>以下链接可能有助于您找到问题的答案。



http://msdn.microsoft.com/en-us/library/bb738523.aspx [ ^ ]



http://msdn.microsoft.com/en-us/library/bb738540.aspx [ ^ ]



http://www.packtpub.com/article/entity-framework-tutorial-table-of-内容 [ ^ ]



http://dotnetshoutout.com/Working-with-transactions-in-Entity-Framework-and-LINQ-to-SQL [ ^ ]



希望这会有所帮助!






您的方法看起来不错,但是使用ObjectContext实例上的SaveChanges,我希望你使用SaveChanges(false)而不是SaveChanges() -



背后的原因是如果其中一个操作如果使用SaveChanges(false),那么在事务内部导致错误,那么你的上下文不会失去实体的状态。



因此你可以继续/采取纠正措施任何错误的情况与SaveChanges()的情况不同。





我希望这有帮助!



问候,

-Vinayak


检查此示例应用程序:



初学者实体框架 [ ^

How to use Transaction in Entity FrameWork? I read some links on Stackoverflow : http://stackoverflow.com/questions/815586/entity-framework-using-transactions-or-savechangesfalse-and-acceptallchanges[^]
But I have 3 table and it means that I have 3 entities:

CREATE TABLE Personel 
(PersonelID integer PRIMARY KEY identity not null, 
Ad varchar(30), 
Soyad varchar(30),
Meslek varchar(100),
DogumTarihi datetime,
DogumYeri nvarchar(100),
PirimToplamı float);
Go

create TABLE Prim
(PrimID integer PRIMARY KEY identity not null,
PersonelID integer Foreign KEY references Personel(PersonelID),
SatisTutari int,
Prim float,
SatisTarihi Datetime);
Go

CREATE TABLE Finans 
(ID integer PRIMARY KEY identity not null, 
Tutar float);



Personel, Prim, Finans are my tables. If you look at Prim table you can see Prim as float value. If I write not float value a textbox my transaction must run.

using (TestEntities testCtx = new TestEntities())
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    // do someyihng...
                    testCtx.Personel.SaveChanges();
                    // do someyihng...
                    testCtx.Prim.SaveChanges();
                    // do someyihng...
                    testCtx.Finans.SaveChanges();
                     scope .Complete();
                        success = true;

                }
            }


How can I do that?

解决方案

Following links may be useful to find answer of your question.

http://msdn.microsoft.com/en-us/library/bb738523.aspx[^]

http://msdn.microsoft.com/en-us/library/bb738540.aspx[^]

http://www.packtpub.com/article/entity-framework-tutorial-table-of-contents[^]

http://dotnetshoutout.com/Working-with-transactions-in-Entity-Framework-and-LINQ-to-SQL[^]

Hope this will help!


Hi,

Your approach looks ok, but while using the SaveChanges on top of ObjectContext instance, i would prefer you to use SaveChanges(false) than SaveChanges() -

The reason behind that is if one of the operation inside the transaction results in error, then your context wont loose the states of an entity if you use SaveChanges(false).

Hence you can continue/take corrective action in case of any error which is not the case with SaveChanges().


I hope this helps!.

Regards,
-Vinayak


Check this sample application:

Entity Framework for Beginners[^]


这篇关于如何在实体框架中使用交易?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆