WebMisCentral-Client 适配MySql数据库

由于本身WebMisCentral采用的是EF5.0,所以适配起来还是非常简单的,下面看操作:

1.ElegantWM.WebUI层中(或者ElegantWM.DAL)通过NUGET下载MySQL.Data 6.7.5

2.修改Web.config如下:

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=152368
  -->
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging"/>
    </sectionGroup>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <log4net configSource="log4netFile.xml"></log4net>
  <connectionStrings>
    <!--主数据库,支持多数据库-->
    <add name="SqlServerDB" providerName="System.Data.SqlClient"  connectionString="Server=localhost;Uid=sa;Pwd=o;DataBase=CN9295;" />
    <add name="DefaultDB" providerName="MySql.Data.MySqlClient"  connectionString="Data Source=localhost;Port=3306;Initial Catalog=WMC;uid=assp;pwd=assp123;Character Set=utf8;" />
  </connectionStrings>
  <appSettings>
    <!--系统名称-->
    <add key="SysName" value="WMC-Client" />    
    <!--请到http://saas.chinacloudtech.com注册账号-->
    <add key="GroupCode" value="" />
    <add key="SysId" value="" />
    <add key="SSO" value="http://saas.chinacloudtech.com" />
    
    <add key="webpages:Version" value="2.0.0.0"/>
    <add key="webpages:Enabled" value="true"/>
    <add key="PreserveLoginUrl" value="true"/>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
  </appSettings>
  
  <system.web>
    .....
  </system.web>
  <system.webServer>
    ......
  </system.webServer>
  <runtime>
    ........
  </runtime>
  <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient" />
      <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.7.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data>
</configuration>

注意上面蓝色的部分是新增的,其他的都不用动。

其中:connectionStrings里你可以随便加不同类型或同类型的多个数据库连接串,因为在WMC中数据库连接串是细化到对象的,不同对象可以拥有不同的连接串,即操作不同的数据库

3.修改ElegantWM.DAL的DBContext.cs,改成如下:

namespace ElegantWM.DAL
{
    public class DB : DbContext
    {
        //public static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        //配置连接串,默认数据库DefaultDB
        public DB(string _ConnectStr)
            : base(_ConnectStr)
        {
            //这是以前的做法
            //Database.Connection.ConnectionString = ConfigurationManager.ConnectionStrings[_ConnectStr].ToString();
            Database.SetInitializer<DB>(null);
            //this.Configuration.LazyLoadingEnabled = false;
        }

4.遗憾的是MySql里的timestamp RowVersion在C# EF里支持不好,需要将byte[] 修改成 DateTime,故你需要将ElegantWM.EntityModel里面的Entity.cs和IEntity.cs中的RowVersion类型修改为DateTime,即可。

5.OK,你可以使用MYSQL了

6.MySql监控EF SQL的工具EFProf.exe,收费的,免费30天试用,用起来还是非常方便强大的。

原文地址:https://www.cnblogs.com/qidian10/p/3732017.html