[英]Understanding Transactions in Entity Framework
Hi I am trying to use transactions along with Entity Framework. 嗨,我试图与实体框架一起使用交易。 With so much information available online on the different ways to implement transactions I must say I am a bit confused on the right way.
由于在线提供了大量有关实施交易的不同方式的信息,我必须说我在正确的方式上有点困惑。 I have an example database with two tables Employee and Company.
我有一个带有两个表Employee和Company的示例数据库。 The Employee table has a foreign key referring to the company Id.
Employee表有一个引用公司Id的外键。 Considering I want to implement a transaction where I insert a record to the Company table and then a record to the Employee table and I want to do this so that records are inserted only if both are successful I have the following code.
考虑到我想要实现一个事务,我将一条记录插入到Company表中,然后一条记录到Employee表,我想这样做,这样只有两个记录都成功时才插入记录我有以下代码。
public void addCompanyToDatabase()
{
using (var context = new myTestEntities())
{
context.Connection.Open(); //added this as was getting the underlying
//provider failed to open
using (TransactionScope scope = new TransactionScope())
{
try
{
Company c = new Company();
c.Name = "xyz";
context.Companies.AddObject(c);
context.SaveChanges();
//int a = 0;
//int b = 5 / a;
Employee e = new Employee();
e.Age = 15;
e.Name = "James";
e.CompanyId = c.Id;
context.Employees.AddObject(e);
context.SaveChanges();
scope.Complete();
}
catch (Exception ex)
{
Console.WriteLine("Exception Occurred");
}
}
}
}
I wanted to know if this was the right way of implementing transactions. 我想知道这是否是实现交易的正确方法。 If it is then what is the use of the
SaveChanges(false)
and the scope.AcceptAllChanges()
functions. 如果是那么使用
SaveChanges(false)
和scope.AcceptAllChanges()
函数是什么。 Any information would be helpful. 任何信息都有帮助。
In your case you don't need to manage any connection or transaction: Entity Framework will do this for you. 在您的情况下,您不需要管理任何连接或事务:实体框架将为您执行此操作。 When you don't supply EF with an opened connection (but with a connection string), it will open a connection and start a transaction during the call to
context.SaveChanges()
. 如果不为打开的连接提供EF(但使用连接字符串),它将打开连接并在调用
context.SaveChanges()
期间启动事务。 When something fails during that call, the transaction will be rolled back. 在该调用期间出现故障时,将回滚该事务。
In other words, your method can simply look like this: 换句话说,您的方法可以简单地看起来像这样:
public void addCompanyToDatabase()
{
using (var context = new myTestEntities())
{
Company c = new Company();
c.Name = "xyz";
context.Companies.AddObject(c);
Employee e = new Employee();
e.Age = 15;
e.Name = "James";
e.CompanyId = c.Id;
context.Employees.AddObject(e);
// Only call SaveChanges last.
context.SaveChanges();
}
}
1-this service (i think transaction service) must be run in client to support TransactionScope 1 - 此服务 (我认为交易服务)必须在客户端运行才能支持TransactionScope
2-Usefull when you have tow or more databases in your application and you want all databases update transactional(such as change connection string of your context). 2 - 当您在应用程序中有两个或更多数据库并且希望所有数据库更新事务时(例如更改上下文的连接字符串)时有用。
3-When you have a database is better use SaveChanges() that internally implement transaction. 3 - 当你有一个数据库时,最好使用内部实现事务的SaveChanges()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.