One-time modification of the closed source Entity Provider assembly to be compatible with the new EntityFramework process, assembly

Source: Internet
Author: User
Tags mscorlib

One-time modification of the closed source Entity Provider assembly to be compatible with the new EntityFramework process, assembly

After reading this article, you will know how to directly modify a DLL without the source code to remove the strong naming restrictions on the DLL, add your "friend assembly" directly on the Assembly (a special Attribute that is applied to the Assembly so that the internal type in the Assembly can be directly called by other assembly) ". Similarly, you can use this method to directly modify the Assembly to achieve the desired purpose.

In a bank project, the customer requested to use their existing Teradata database. The project team first thought of nhib.pdf, but hardly found any information about nhib.pdf's support for Teradata databases, so I threw the question to me.

I have not found any relevant information. The strange thing is that Java-based hibernate supports Teradata. Why didn't nhibect perform Teradata dialect transplantation during the migration? Is there any technical restriction? On the one hand, I expressed this question in the official nhib.pdf Group. On the other hand, I found that Entity Framework supports the Teradata database and informed the project team of this message.

The next day, I saw a reply in the nhibbench Group, "maybe only because no one has implemented it ". In this way, I also dispelled my last concerns. I made a NHibernate version implementation by referring to hibernate teradata dialect of Java. Since there is no specific environment for the teradata database of the project team, they can only send the test to the project team. I insist on using nhib.pdf because the company's framework has mature encapsulation on nhib.pdf. If EntityFramework is used in another way, adaptation is required.

Finally, the project team decided to use EntityFramework, but wanted to use the latest EntityFramework, but found that the latest EntityFramework 6.1 had a problem with Teradata support, so I threw the problem to me.

The reason is not that EntityFramework is not compatible with Teradata. EntityFramework supports multi-database. The principle is to abstract common databases and then implement different databases (Entity Provider ), some database implementations are officially provided by EntityFramework (such as SQL Server Entity Provider). Teradata database officially provides support for EntityFramework, but it may be because EntityFramework was modified later, as a result, the old Teradata Entity Provider cannot be used normally on EntityFramework 6.1, and the Teradata official website does not update the Provider in time.

 

Well, let's leave aside any distractions and try it with a research attitude:

 

Teradata for. NETInvolved dll

Teradata. Client. Provider. dll

The underlying driver of the Teradata database. This Assembly adopts a strong name. And the following Teradata. client. entity. dll youyuan (this is the internal class in this Assembly is visible to the youyuan assembly, Teradata. client. provider. the internal class in the dll is Teradata. client. entity. dll visible ).

Teradata. Client. Entity. dll

Reference Teradata. Client. Provider. dll. EntityFramework Provider of Teradata implements the interface specified by EntityFramework, so that EntityFramework can use it to manipulate the Teradata database.

 

Solution:

Try to wrap a layer on its original Entity Provider to be compatible with the new version of EntityFramework. Practice has proved that, although the Code looks similar, however, the new EntityFramework changes the namespace and assembly of a large number of classes, and there is no way to handle the conversion between these new and old types. So I gave up this solution.

Solution B:

Modify the source code of Teradata. Client. Entity. dll (Entity Provider) to be compatible with the new EntityFramework.

No Source Code is available on the Internet. The Source Code of Teradata. Client. Entity. dll is obtained through decompilation, and more than a dozen errors are reported for compiling the Source Code, but it can be corrected. The Code compiled in the most out-of-the-box mode has no errors.

But the compilation still fails, because Teradata. client. entity. the dll is Teradata. client. provider. dll, Teradata. client. entity. dll directly references Teradata. client. provider. the internal class in the dll, and Teradata. client. provider. the meta-Assembly specified by dll is strongly named Teradata. client. entity. dll, decompiled Teradata. client. entity. the dll loses its strong name and fails to be compiled.

 

[Figure Teradata. Client. Provider. dll: the user meta Assembly specified by Teradata. Client. Entity. dll is strongly named.]

There are two solutions:

1. decompile Teradata. Client. Provider. dll, get the source code, remove the strong naming friends of Teradata. Client. Entity. dll, and then compile. (A large number of errors are reported after decompilation)

2. directly use a tool to modify Teradata. Client. Provider. dll, and change the strong naming friends of Teradata. Client. Entity. dll to weak naming friends.

Try two methods:

1. Enter the following Command at the Developer Command Prompt for VS2013 Command Prompt:

: Decompile the DLL to get the IL

Ildasm Teradata. Client. Provider. dll/output = I. il

Open and find the following:

. Assembly Teradata. Client. Provider

{

. Custom instance void [mscorlib] System. Runtime. CompilerServices. InternalsVisibleToAttribute:. ctor (string) = (

01 00 81 62 54 65 72 61 64 61 74 61 2E 43 6C 69 //... bTeradata. Cli

65 6E 74 2E 45 6E 74 69 74 79 2C 20 50 75 62 6C // ent. Entity, Publ

69 63 4B 65 79 3D 30 30 32 34 30 30 30 30 30 34 // icKey = 0024000004

38 30 30 30 30 30 39 34 30 30 30 30 30 30 36 // 8000009400000006

30 32 30 30 30 30 30 30 32 34 30 30 30 35 32 // 0200000024000052

35 33 34 31 33 31 30 30 34 30 30 30 30 31 // 5341310004000001

30 30 30 31 30 30 63 39 66 62 31 62 35 33 36 // 000100c9fcb1b536

61 65 36 31 31 30 32 61 37 30 36 61 31 65 38 30 // ae61102a706a1e80

31 65 36 32 65 64 34 37 35 39 32 37 39 36 34 35 // 1e62ed4759279645

37 65 30 36 33 36 39 61 63 61 31 34 62 65 66 // 7e06108acac14bef

38 34 66 65 36 61 33 32 39 39 34 34 31 34 63 // 84fe6a41546414c

39 65 30 35 65 32 65 62 66 65 64 66 30 36 61 66 // 9e05e2ebfedf06af

33 66 39 36 32 37 36 64 34 32 31 35 32 38 30 37 // 3f96276d42152807

36 39 35 37 63 35 30 32 33 35 63 36 65 38 31 37 // 6957c50235c6e817

64 62 64 34 37 66 64 32 35 66 35 37 33 61 34 // dbd47fd25f5773a4

62 62 65 62 31 30 61 62 65 61 38 36 34 36 31 // bbeb10abeea86461

33 34 33 34 66 34 39 63 38 36 30 63 35 34 32 38 // 3434f49c860c5428

31 31 66 36 35 61 30 38 35 65 35 33 34 65 65 34 // 11f65a085e534ee4

34 38 37 33 37 31 33 31 61 37 64 38 62 31 33 63 // 415371_a7d8b13c

34 34 33 34 63 39 36 63 37 61 35 39 38 65 36 // 44334c96c7a598e6

65 65 36 38 65 63 61 66 64 34 66 37 63 61 31 33 // ee68ecafd4f7ca13

38 37 33 38 37 35 34 64 65 61 38 65 36 33 30 // 87338754dea8e630

31 61 66 65 63 38 00 00) // 1afec8 ..

.....

}

PublickKey is followed by a strongly-named Public Key. Delete it in the Editor:

. Custom instance void [mscorlib] System. Runtime. CompilerServices. InternalsVisibleToAttribute:. ctor (string) = (

01 00 16 54 65 72 61 64 61 74 61 2E 43 6C 69 65 //... Teradata. Clie

6E 74 2E 45 6E 74 69 74 79 00) // nt. Entity ..

 

In this way, the friends of Teradata. Client. Provider. dll to Teradata. Client. Entity. dll are not strongly named.

Then, enter the following Command at the Developer Command Prompt for VS2013 Command Prompt:

: Compile IL as DLL

Ilasm.exe I. il/DLL/OUTPUT = Teradata. Client. Provider. dll

[After the graph is modified]

 

In this case, Teradata. client. entity. the dll can be compiled, which is not complete yet. You also need to compile Teradata. client. entity. the old EntityFramework Provider implementation in dll is modified to the new implementation (basically, you can replace the namespace in batches ).

 

Add it to the project to rereference and modify it. the EntityFramework Entity Provider in config implements the EntityFramework Provider class in the project we just decomcompiled: "Teradata. client. entity. tdProviderServices, Teradata. client. entity"

Run the project.

 

Finally, I think it is more important than knowing how to solve a problem. It is more important than thinking about how to solve a problem, it is a matter of calm in the face of various thorny, troublesome, and chaotic problems, big and small.




Related Article

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.