Entity Framework 学习中级篇5—使EF支持Oracle9i

 

从Code MSDN上下载下来的EFOracleProvider不支持Oracle9i.但是,目前我所使用的还是Oracle9i。为此,对EFOracleProvider修改了以下,以便使其支持Oracle9i.

下面说说具体修改地方.(红色部分为添加或修改的代码部分)

一,修改EFOracleProvider

 

1,修改EFOracleProviderManifest.cs类文件,

 

internal const string TokenOracle9i = "9i";//add by xray2005

        internal const string TokenOracle10g = "10g";

        internal const string TokenOracle11g = "11g";

以下两个地方,不修改也是可以的.但考虑目前我主要是使用9i,所以也就修改成9i了.

        private EFOracleVersion _version = EFOracleVersion.Oracle9i; //EFOracleVersion.Oracle11g;

        private string _token = TokenOracle9i; //TokenOracle10g;

 

2,修改EFOracleVersion.cs类文件,如下代码所示:

namespace EFOracleProvider

{

    using System;

    /// <summary>

    /// This enum describes the current storage version

    /// </summary>

    internal enum EFOracleVersion

    {

         Oracle9i = 9, //add by xray2005

        /// <summary>

        /// Oracle10g

        /// </summary>

        Oracle10g = 10,

 

        /// <summary>

        /// Oracle 11g

        /// </summary>

        Oracle11g = 11,

        // higher versions go here

    }

    /// <summary>

    /// This class is a simple utility class that determines the version from the

    /// connection

    /// </summary>

    internal static class EFOracleVersionUtils

    {

        /// <summary>

        /// Get the version from the connection.

        /// </summary>

        /// <param name="connection">current connection</param>

        /// <returns>version for the current connection</returns>

        internal static EFOracleVersion GetStorageVersion(EFOracleConnection connection)

        {

            string serverVersion = connection.ServerVersion;

            if (serverVersion.StartsWith("9."))

            {

                return EFOracleVersion.Oracle9i; //add by xray2005

            }

            else if (serverVersion.StartsWith("10."))

            {

                return EFOracleVersion.Oracle10g;

            }

            else if (serverVersion.StartsWith("11."))

            {

                return EFOracleVersion.Oracle11g;

            }

            throw new ArgumentException("Could not determine storage version; " +

                    "a valid storage connection or a version hint is required.");

        }

        internal static string GetVersionHint(EFOracleVersion version)

        {

            switch (version)

            {

                case EFOracleVersion.Oracle9i:

                    return EFOracleProviderManifest.TokenOracle9i; //add by xray2005

                case EFOracleVersion.Oracle10g:

                    return EFOracleProviderManifest.TokenOracle10g;

                case EFOracleVersion.Oracle11g:

                    return EFOracleProviderManifest.TokenOracle11g;

                default:

                    throw new ArgumentException("Could not determine storage version; " +

                            "a valid storage connection or a version hint is required.");

            }

        }

        internal static EFOracleVersion GetStorageVersion(string versionHint)

        {

            if (!string.IsNullOrEmpty(versionHint))

            {

                switch (versionHint)

                {

                    case EFOracleProviderManifest.TokenOracle9i:

                        return EFOracleVersion.Oracle9i; //add by xray2005

                    case EFOracleProviderManifest.TokenOracle10g:

                        return EFOracleVersion.Oracle10g;

                    case EFOracleProviderManifest.TokenOracle11g:

                        return EFOracleVersion.Oracle11g;

                }

            }

            throw new ArgumentException("Could not determine storage version; " +

                    "a valid storage connection or a version hint is required.");

        }

 

        internal static bool IsVersionX(EFOracleVersion storageVersion)

        {

            return storageVersion == EFOracleVersion.Oracle9i || storageVersion == EFOracleVersion.Oracle10g ||

                storageVersion == EFOracleVersion.Oracle11g; //add by xray2005

        }

    }

}

二,使用EFOracleProvider

修改完毕后,编译一下.如果是自己下载的源代码编译的,那么编译后的EFOracleProvider自动已经在GAC注册了.如果是手动注册EFOracleProvider到GAC,那么命令如下:

gacutil –I “EFOracleProvider.dll”

其中gacutil.exe位于:系统盘符号:\Program Files\Microsoft SDKs\Windows\v6.0A\bin下面。

接下来,我们需要做的就是,把这个EFOracleProvider添加到Machine.config中.

第一步,找到Machine.config文件.该文件的位置在:

系统盘符号:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG下面.

第二步,用打开Machine.config文件,在DbProviderFactories配置节点,增加EFOracleProvider的配置,如下所示:

<DbProviderFactories>

      <add name="Odbc Data Provider" invariant="System.Data.Odbc" description=".Net Framework Data Provider for Odbc" type="System.Data.Odbc.OdbcFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

      <add name="OleDb Data Provider" invariant="System.Data.OleDb" description=".Net Framework Data Provider for OleDb" type="System.Data.OleDb.OleDbFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

      <add name="OracleClient Data Provider" invariant="System.Data.OracleClient" description=".Net Framework Data Provider for Oracle" type="System.Data.OracleClient.OracleClientFactory, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

      <add name="SqlClient Data Provider" invariant="System.Data.SqlClient" description=".Net Framework Data Provider for SqlServer" type="System.Data.SqlClient.SqlClientFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

      <add name="Microsoft SQL Server Compact Data Provider" invariant="System.Data.SqlServerCe.3.5" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=3.5.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />

     <add name="EF Oracle Data Provider" invariant="EFOracleProvider" description="EF Provider for Oracle" type="EFOracleProvider.EFOracleProviderFactory,EFOracleProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=def642f226e0e59b" />

</DbProviderFactories>

第三步,保存即可.

 

接下来,简单的介绍一下,如何使用这个EFOralceProvider.

第1步:在命令行窗口,将目录定位到提示符, 系统盘符:\WINDOWS\Microsoft.NET\Framework\v3.5.如下所示(是我电脑上的目录):

第2步,输入相应的生成参数.如下图所示:

“data source=test;user id=xray;password= 123成你自己的对应的参数即可.

确定之后,就可以看到生成的结果了,同时会有写信息出来,如下示:

 

 

至此,EdmGen就为我们生成需要的文件.生成的文件如下所示:

l           TestEFModel.csdl

l           TestEFModel.msl

l           TestEFModel.ssdl

l           TestEFModel.ObjectLayer.cs

l           TestEFModel.Views.cs

然后,通过EdmGen2工具, 使用刚刚生成的TestEFModel .csdl, TestEFModel .msl, TestEFModel .ssdl三个文件来生成一个模型.

命令如下:

Edmgen2.exe /toedmx TestEFModel.csdl TestEFModel.msl TestEFModel.ssdl

确定之后,该工具就会为我们生成一个TestEFModel.edmx文件了.

然后,把这个文件加入到我们的项目中,同时修改项目的App.Config文件连接字符串,如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <connectionStrings>
    <add name="NorthwindEFModelContext" 
         connectionString="provider=EFOracleProvider;
                           metadata=res://*/TestEFModel.csdl|res://*/TestEFModel.ssdl|res://*/TestEFModel.msl;
                           Provider Connection String='data source=test;user id=xray;password=1111'" 
         providerName="System.Data.EntityClient" />
 </connectionStrings>
</configuration>

最后保存.

至此,修改EFOracleProvider并使用,介绍完毕。

最后提供几个连接,以方便大家学习研究:

l           Code.MSDN上的EFOracleProvider

l           经过我修改后,支持Oracle9i的EFOracleProvider.dll

l           EdmGen2.exe 

posted @ 2009-06-05 10:39  ♂风车车  阅读(9110)  评论(12编辑  收藏  举报