简体   繁体   English

在单个事务内一起建立实体框架实体添加和ADO.Net存储过程INSERTS

[英]Establishing Entity Framework Entity addition & ADO.Net stored procedure INSERTS together inside a single Transaction

I have an application where I am using Entity Framework or handle data from database tables. 我有一个使用实体框架或处理数据库表中数据的应用程序。 There is a main table, CUSTOMER which is is linked to 3 other tables through foreign keys. 有一个主表CUSTOMER ,它通过外键链接到其他3个表。 The linked tables are 链接的表是

RENTAL
PAYMENTS
VEHICLES

I am able to add / edit / delete entities on all these 4 tables using EF. 我可以使用EF在所有这4个表上添加/编辑/删除实体。

I would like to change the current scenario (where EF is used to handle all DB transaction) to something like this. 我想将当前方案(使用EF来处理所有数据库事务)更改为类似的情况。

After I create a new entity and when try to save the entities (5 new entities/records) back to the database, I would like to use a stored procedure to add the new entity to VEHICLES model and use EF to save entities to all other models - and still gat all this down under one single transaction. 创建新实体并尝试将实体(5个新实体/记录)保存回数据库后,我想使用存储过程将新实体添加到VEHICLES模型中,并使用EF将实体保存到所有其他实体中模型-并在一次交易中就将所有这些降低了。

Also, I have to wait till the EF call is completed, since I need the new CUSTOMER_ID created as part of EF save to be passed on as an input parameter to the stored procedure to save VEHICLES entity. 另外,我必须等到EF调用完成,因为我需要将作为EF保存的一部分创建的新CUSTOMER_ID作为输入参数传递给存储过程,以保存VEHICLES实体。

I don't want to call the stored procedure from outside the transaction, thus if the Stored Procedure call fails, the changes made to the database through EF also are rolled back. 我不想从事务外部调用存储过程,因此,如果存储过程调用失败,则通过EF对数据库所做的更改也会回滚。

Anyone have any idea how to get this accomplished? 任何人都知道如何实现这一目标吗?

PS : Not looking for any code samples; PS:不查找任何代码示例; a high level illustration will help. 高水平的插图会有所帮助。

I am using Entity Framework 5, .Net v4.5, VS 2013, Oracle 11g, ODP.Net 我正在使用Entity Framework 5,.Net v4.5,VS 2013,Oracle 11g,ODP.Net

Map stored procs to Entity 1) Create the stored procedure(s) 2) Update your data model to include the stored procedure(s) - "Update Model From Database" menu via EF 6.x 将存储的过程映射到实体1)创建存储的过程2)更新数据模型以包括存储的过程-通过EF 6.x的“从数据库更新模型”菜单

3) Then find the Entity (or create a new Entity) in the Entity Data Model and hook up Stored Procs to Insert, Update, and Delete Functions - right click the Entity and select "Stored Procedure Mapping" menu 3)然后在实体数据模型中找到该实体(或创建一个新的实体)并连接存储过程以插入,更新和删除函数-右键单击该实体,然后选择“存储过程映射”菜单

4) Use LinqToEntity to add/modify the entities with Database.BeginTransaction() then call SaveChanges() on context, then Commit or Rollback. 4)使用LinqToEntity通过Database.BeginTransaction()添加/修改实体,然后在上下文上调用SaveChanges(),然后提交或回滚。

Note: Starting with EF6 Database.ExecuteSqlCommand() by default will wrap the command in a transaction if one was not already present. 注意:默认情况下,从EF6 Database.ExecuteSqlCommand()开始,会将命令包装在一个事务中(如果尚不存在)。 There are overloads of this method that allow you to override this behavior if you wish. 此方法有很多重载,允许您在需要时覆盖此行为。 Also in EF6 execution of stored procedures included in the model through APIs such as ObjectContext.ExecuteFunction() does the same (except that the default behavior cannot at the moment be overridden). 同样,在EF6中,通过诸如ObjectContext.ExecuteFunction()之类的API执行包含在模型中的存储过程也是如此(但当前无法覆盖默认行为)。 In either case, the isolation level of the transaction is whatever isolation level the database provider considers its default setting. 无论哪种情况,事务的隔离级别都是数据库提供者认为其默认设置的隔离级别。 By default, for instance, on SQL Server this is READ COMMITTED. 例如,默认情况下,在SQL Server上,这是READ COMMITTED。 https://msdn.microsoft.com/en-us/data/dn456843.aspx https://msdn.microsoft.com/en-us/data/dn456843.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM