Cross-database operations in EF5.0

Source: Internet
Author: User

Previously in the MVC + EF project, is a database, a dbcontext, so has not been considered in the MVC+EF environment for multiple database operation problems. When it's time to use it, it turns out that the problem is not small (the key is a pit). Just say the solution to the problem:

1) from the previous experience: in ADO, to operate multiple databases, then write more than two SqlHelper bar, or package, can dynamically modify the link string. So you think you can build multiple ADO data models here? So there are two of files: SEALDBMODEL.EDMX and UCMSDBMODEL.EDMX.

2) Sealdbmodel is the primary, so it is inherited in the server for its processing. Ucmsdbmodel is also used at the same time. In order to do the testing, the following code is used:

namespaceseal_services{ Public classSeal_testserver:baseserver<seal_test>, Iseal_usersserver { Public voidtttt () {Try{seal_test Test=Newseal_test () {name="1111", Age=1                    }; Base.                    Addentity (test); Base. Db.                    SaveChanges (); Baseucmsserver<au_Role> Ucmsserver =NewBaseucmsserver<au_role>(); Au_role Role=NewAu_role (); Role= Ucmsserver. Findentitie (c = c.id = =7); Role.id=Ten;//make an exceptionRole.role_name ="bbbbbbbbbbb"; Ucmsserver.                    Updateentity (role);                Ucmsserver.db.SaveChangesForUCMS (); }                Catch(System.Exception ex) {Throwex; }        }    }}    

3) This code is problematic, when you want to operate different databases at the same time, and there is data validity, you will find the previous one success, the latter one failed. What do we do? The EF's transaction mechanism is obviously not enough to meet the requirements of the program, what to do? Add TransactionScope.

4) Then there is this piece of code:

namespaceseal_services{ Public classSeal_testserver:baseserver<seal_test>, Iseal_usersserver { Public voidtttt () {using(TransactionScope scope =NewTransactionScope ()) {                Try{seal_test Test=Newseal_test () {name="1111", Age=1                    }; Base.                    Addentity (test); Base. Db.                    SaveChanges (); Baseucmsserver<au_Role> Ucmsserver =NewBaseucmsserver<au_role>(); Au_role Role=NewAu_role (); Role= Ucmsserver. Findentitie (c = c.id = =7); Role.role_name="bbbbbbbbbbb"; Ucmsserver.                    Updateentity (role);                    Ucmsserver.db.SaveChangesForUCMS ();                Scope.complete (); }                Catch(System.Exception ex) {Throwex; }            }        }    }}

5) There is still a problem. When processing the second server, it throws an exception "the underlying provider failed on Open ." To be sure, however, the database link string is correct. But the error, which is why? "This is a hole in my computer environment."

6) after analysis (turned over countless Baidu the same post ...), think of, will there be SQL configuration related? caused the use of TransactionScope failed?

7) When I open SQL Server Configuration Manager, I see the SQL Server service, which shows that the remote procedure call failed . This one??? If you fail, let it be right. So: Open Control Panel-Uninstall find Express LocalDB. Firmly to "unload" it. Return to the program, and then execute. The result is ... Success!!!

PostScript :

1) using cross-database operations in EF, consider using the [build multiple ADO Entity Data Model] provided here. When doing TransactionScope business, be sure to pay attention to [Express LocalDB].

2) in the process of solving EF cross-database operations, [SQL Synonym] is also considered. But it turns out that this is a completely unworkable practice, whether in EF5.0 or EF6.0.

problem Environment : Win8.1, SQL Server R2, Visual Studio Professional Update3

Cross-database operations in EF5.0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.