Fork me on GitHub
Microsoft Enterprise Library 5.0 如何集成MySQL数据库

今天在网上找了一下Microsoft Enterprise Library 5.0 如何集成MySQL数据库, 结果只找到了entlib 4.1的扩展模块,

http://entlibcontrib.codeplex.com/ , 按照上面的说明配制后还是不成功。

于是只好手动修改一下来支持entlib 5.0

修改后的项目:   MySqlDAAB.rar 

MySql.Data.dll:  MySql.Data.dll

1. 修改MySqlDatabaseAssembler.cs文件

public class MySqlDatabaseData : DatabaseData
    {
        
#region Public Methods
        
public MySqlDatabaseData(ConnectionStringSettings connectionStringSettings, IConfigurationSource configurationSource)
            : 
base(connectionStringSettings, configurationSource)
        {
        }
        
#endregion

        
public override System.Collections.Generic.IEnumerable<Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.TypeRegistration> GetRegistrations()
        {
            
yield return new TypeRegistration<Database>(
                () 
=> new MySqlDatabase(
                    ConnectionString,
                    Container.Resolved
<IDataInstrumentationProvider>(Name))) {
                        Name 
= Name,
                        Lifetime 
= TypeRegistrationLifetime.Transient
                    };
        }
    }

2. 修改MySqlDatabase.cs文件中的构造函数和在MySqlDatabase类上加上配制属性

  [ConfigurationElementType(typeof(MySqlDatabaseData))]
    
public class MySqlDatabase : Database
    {       

        
public MySqlDatabase(string connectionString)
            : 
base(connectionString, MySqlClientFactory.Instance)
        {
        }        
        
public MySqlDatabase(string connectionString, IDataInstrumentationProvider instrumentationProvider)
            : 
base(connectionString, MySqlClientFactory.Instance, instrumentationProvider)
        {
        }        

3. 编译MySqlDAAB项目

4. 在项目中引用MySqlDAAB项目

5. 配制文件中如下配制: 

    <configSections>
        
<section name="dataConfiguration"
            type
="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings,
                    Microsoft.Practices.EnterpriseLibrary.Data" />
    </configSections>
    <dataConfiguration >
        
<providerMappings>
            
<add name="MySql.Data.MySqlClient"
            databaseType
="EntLibContrib.Data.MySql.MySqlDatabase, MySqlDAAB" />
        
</providerMappings>
    
</dataConfiguration>
    
<system.data>
        
<DbProviderFactories>
            
<add
                name
="My Sql Data Provider Factory"
                invariant
="MySql.Data.MySqlClient"
                description
=""
                type
="MySql.Data.MySqlClient.MySqlClientFactory" />
        
</DbProviderFactories>
    
</system.data>
    
<connectionStrings>
      
<add name="TestDB_MySql" 
      connectionString="server=127.0.0.1;database=test;User Id=root;Password=****;Persist Security Info=True;"
           providerName
="MySql.Data.MySqlClient"/>
    
</connectionStrings>

 这样就可以使用MySQL数据库了。

posted on 2011-03-31 23:50  HackerVirus  阅读(401)  评论(0编辑  收藏  举报