SQLite in C#, "v2.0.50727" appears... Cannot load the assembly in 4.0 runtime

Today, when SQLite was used for Form development in C#, after loading "System.Data.SQLite.DLL", the following error occurred:
"Mixed mode assembly was generated for the runtime of "v2.0.50727". When other information is configured, the assembly cannot be loaded in the 4.0 runtime"

The solution is as follows:
The method of calling is to obtain the previously used database connection from the sqlite database. I didn't pay attention at that time. I was preparing to set a breakpoint and then step through the debugging. The result was that the breakpoint could not enter the method body. When I looked at the method body, I found a problem, that is, the existing System.Data.Sqlite data access provider is developed for the .NET2.0 environment (the latest version is 1.0.66.0, released on April 18, 2010). At present, the official does not provide the latest data access support for .NET4.

Since this problem occurs, it must be a Google search solution. After all, it is impossible for Microsoft to be unable to access the .NET2.0 assembly because of the program upgraded to .NET4.0. Later, I found a solution on the famous stackoverflow.com, which is to add a configuration section in app.config: startup

<startup useLegacyV2RuntimeActivationPolicy="true">

<supportedRuntime version="v4.0"/>

</startup>

The meaning of this configuration section is (referenced from MSDN, specific address: http://msdn.microsoft.com/zh-cn/library/bbx34a2h.aspx ):

Enable the .NET Framework version 2.0 runtime activation policy, which is to load all assemblies by using the latest supported runtime.
Note: Due to the characteristics of the config configuration file, if there is a configSections node in the config configuration file, you must put configSections in one, otherwise an exception will be raised: the configuration system failed to initialize

In the original .NET2.0 and .NET3.5, due to the nature of the program operating environment is still .NET2.0, but to .NET4.0 due to the version update of the entire assembly, the assembly written by .NET2.0 was used before The compatibility problem mentioned above will occur when the assembly of .NET4.0 continues to interoperate.

Through MSDN, we can know that the useLegacyV2RuntimeActivationPolicy attribute in the startup configuration section is newly added in .NET4.0, and the default is false, which means: Use the default .NET Framework 4 activation strategy, which will load .NET Framework 4 Assemblies created by using Common Language Runtime (CLR) version 4, and assemblies created by earlier versions of the CLR by using the highest supported CLR version lower than version 4.

Now if you want to use .NET2.0 and .NET3.5 programs in the .NET4.0 environment, you must set useLegacyV2RuntimeActivationPolicy to true, and also note that you need to add the supportedRuntime configuration to the bytes of the startup configuration section Section, and designated as "v4.0", which means using the .NET4.0 runtime to run the program.

For more startup and its children, you can check MSDN:
startup: http://msdn.microsoft.com/zh-cn/library/bbx34a2h.aspx

supportedRuntime:http://msdn.microsoft.com/zh-cn/library/w4atty68.aspx

Original: http://hi.baidu.com/mubingyun/blog/item/aa4c833d832d7ae03c6d97e4.html

Guess you like

Origin blog.csdn.net/coolhe21cn/article/details/77936145