The value's length for key 'data source' exceeds it's limit of '128'

后端 未结 2 806
心在旅途
心在旅途 2021-01-22 11:59

I know that a very similar question has been asked here, but the answer didn\'t help me.

I am using Entity Framework 6 with the Oracle.ManagerDataAccess.Client.

相关标签:
2条回答
  • 2021-01-22 12:34

    My colleague has found an answer to this problem as follows:

    Add another constructor to the context class to use an existing connection.

    public GlobalAttributeContext(DbConnection existingConnection, bool contextOwnsConnection) 
           : base(existingConnection, true)
    {
    }
    

    This is the complete context class

    namespace OracleTestExeConfigAndConnStr
    {
      [DbConfigurationType(typeof(OracleDBConfiguration))]
      public class GlobalAttributeContext : DbContext
      {
        public DbSet<GlobalAttribute>  GlobalAttributes { get; set; }
    
        static GlobalAttributeContext()
        {
          Database.SetInitializer<GlobalAttributeContext>(null);
        }
    
        public GlobalAttributeContext() : base("OracleDbContext")
        {
        }
    
        public GlobalAttributeContext(string nameOrConnectionString)
               : base(nameOrConnectionString)
        {
        }
    
        public GlobalAttributeContext(DbConnection existingConnection, bool contextOwnsConnection)
               : base(existingConnection, true)
        {
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
          // We have to pass the schema name into the configuration. (Is there a better way?)
          modelBuilder.Configurations.Add(new GlobalAttribute_Config_Oracle("SchemaName")) ;
        }
      }
    }
    

    Create the database connection as a separate step and pass the connection into the context object.

    string connStr = @"User Id=xxxxxxxxxxx;Password=xxxx;Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=VS-ORACLE.xxxxxxxx.de)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl.xxxxxxxx.de)))";
    
    using (var connection = new OracleConnection() { ConnectionString = connStr })
    {
      connection.Open();
      using (var ctx = new GlobalAttributeContext(connection, true))
      {
        var globalAttributes = ctx.GlobalAttributes.ToList();
        foreach (GlobalAttribute ga in globalAttributes)
        {
          Console.WriteLine("Name: {0}, Value: {1}", ga.Attribute, ga.Value);
        }
      }
    }
    

    For completeness, this is the DBConfiguration class, which is specified as an attribute on the context class.

    class OracleDBConfiguration : DbConfiguration
    {
      public OracleDBConfiguration()
      {
        this.SetDefaultConnectionFactory ( new System.Data.Entity.Infrastructure.LocalDbConnectionFactory("v12.0") ) ;
        this.SetProviderServices ( "Oracle.ManagedDataAccess.Client", Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices.Instance ) ;
        this.SetProviderFactory  ( "Oracle.ManagedDataAccess.Client", Oracle.ManagedDataAccess.Client.OracleClientFactory.Instance ) ;
      }
    }
    

    This method works from a DLL without requiring any values in app.config.

    0 讨论(0)
  • 2021-01-22 12:36

    You don't need any Oracle Client to use the tnsnames.ora file.

    Just see this answer (last paragraph) in which folder ODP.NET Managed Driver expects the tnsnames.ora, resp. sqlnet.ora file.

    You can define the alias also in .config file, see Configuring Oracle Data Provider for .NET

    0 讨论(0)
提交回复
热议问题