实体框架代码第一个DbConfiguration for Multiple database

时间:2017-05-04 10:20:45

标签: c# .net sql-server oracle entity-framework

我正在使用EF6和多个数据库(SQL SERVER,ORACLE) 我们正在从SQL Server迁移到Oracle。一切都在SQL服务器上运行。在Oracle中,我们面临修剪char列的问题。我使用此链接trailing-blanks-issue-in-string-joins来解决问题。但默认情况下,此拦截适用于SQL Server。我想将此应用于Oracle。
我所做的是

public class MyConfiguration : DbConfiguration
        {
            public MyConfiguration()
            {
                SetProviderServices("Oracle.ManagedDataAccess.Client", EFOracleProviderServices.Instance);
                SetProviderFactory("Oracle.ManagedDataAccess.Client", OracleClientFactory.Instance);
                AddInterceptor(new StringTrimmerInterceptor());

            }
        }


[DbConfigurationType(typeof(EBS.DAL.Model.MyConfiguration))] 
        public class EBS_GIROdbContext : DbContext
        {

            static EBS_GIROdbContext()
            {
                Database.SetInitializer<EBS_GIROdbContext>(null);

            }
            //NeoSampleGIRODBEntities
            //OracleDbContext
            public EBS_GIROdbContext()
                : base("Name=OracleDbContext")
            {

            }


在GLOBAL.ASAX中

  DbConfiguration.SetConfiguration(new EBS.DAL.Model.MyConfiguration());


它还在申请SQL SERVER。如何为Oracle添加DbConfiguration。 我无法得到它

提前致谢

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。

public sealed class EntityFrameworkConfiguration : DbConfiguration
    {
        public static readonly DbConfiguration Instance = new EntityFrameworkConfiguration();

        EntityFrameworkConfiguration()
        {
            this.SetDefaultConnectionFactory(new OracleConnectionFactory());
            this.SetProviderServices("Oracle.ManagedDataAccess.Client", EFOracleProviderServices.Instance);
             this.AddInterceptor(new StringTrimmerInterceptor());

        }
    }


[DbConfigurationType(typeof(EntityFrameworkConfiguration))] 
    public class EBS_GIROdbContext : DbContext
    {

        static EBS_GIROdbContext()
        {
            Database.SetInitializer<EBS_GIROdbContext>(null);
        }


在Global.ASAX中

 DbConfiguration.SetConfiguration(EntityFrameworkConfiguration.Instance);


此设置将在varchar2数据类型的所有选择查询中添加修剪函数,如此

SELECT
    "Extent1"."LBRCODE" AS "LBRCODE",
    LTRIM(RTRIM("Extent1"."CAT")) AS "CAT",
    LTRIM(RTRIM("Extent1"."CATTYPE")) AS "CATTYPE",