EF 多数据库切换配置(MSSQL/MySql)

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!--
 3   有关如何配置 ASP.NET 应用程序的详细信息,请访问
 4   https://go.microsoft.com/fwlink/?LinkId=301880
 5   -->
 6 <configuration>
 7   <configSections>
 8     <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
 9     <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
10   </configSections>
11   <connectionStrings>
12     <add name="DataModel" providerName="MySql.Data.MySqlClient" connectionString="Data Source=localhost;Initial Catalog=Demo;User ID=root;Password=123456" />
13     <!--<add name="DataModel" connectionString="data source=.;initial catalog=Demo;persist security info=True;user id=sa;password=123456;multipleactiveresultsets=True;application name=EntityFramework" providerName="System.Data.SqlClient" />-->
14   </connectionStrings>
15   <appSettings>
16     <add key="webpages:Version" value="3.0.0.0" />
17     <add key="webpages:Enabled" value="false" />
18     <add key="ClientValidationEnabled" value="true" />
19     <add key="UnobtrusiveJavaScriptEnabled" value="true" />
20   </appSettings>
21   <system.web>
22     <compilation debug="true" targetFramework="4.7.2" />
23     <httpRuntime targetFramework="4.7.2" />
24   </system.web>
25   <runtime>
26     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
27       <dependentAssembly>
28         <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" />
29         <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
30       </dependentAssembly>
31       <dependentAssembly>
32         <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
33         <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
34       </dependentAssembly>
35       <dependentAssembly>
36         <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
37         <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
38       </dependentAssembly>
39       <dependentAssembly>
40         <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
41         <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
42       </dependentAssembly>
43       <dependentAssembly>
44         <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
45         <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
46       </dependentAssembly>
47       <dependentAssembly>
48         <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
49         <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
50       </dependentAssembly>
51       <dependentAssembly>
52         <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
53         <bindingRedirect oldVersion="1.0.0.0-5.2.7.0" newVersion="5.2.7.0" />
54       </dependentAssembly>
55     </assemblyBinding>
56   </runtime>
57   <system.codedom>
58     <compilers>
59       <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
60       <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
61     </compilers>
62   </system.codedom>
63 
64 
65   <entityFramework>
66     <!--MySQL启用下面一段配置项-->
67     <!--<defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6">
68       <parameters>
69         <parameter value="v11.0" />
70       </parameters>
71     </defaultConnectionFactory>-->
72     <!--SQL SERVER 启用下面一行-->
73     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework" >
74       <parameters>
75         <parameter value="mssqllocaldb" />
76       </parameters>
77     </defaultConnectionFactory>
78     <providers>
79       <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
80       <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.10.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
81       </provider>
82     </providers>
83   </entityFramework>
84 
85   <system.data>
86     <DbProviderFactories>
87       <remove invariant="MySql.Data.MySqlClient" />
88       <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.10.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
89     </DbProviderFactories>
90   </system.data>
91 
92 </configuration>

DBContext配置:

this.SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());

或者

DBContext.cs

[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]

posted @ 2019-09-09 10:53  情若天_RunUp  阅读(895)  评论(0编辑  收藏  举报