【问题标题】:Entity Framework with oracle can´t change connection string带有 oracle 的实体框架无法更改连接字符串
【发布时间】:2015-04-15 10:59:13
【问题描述】:

我在 ASP.NET MVC 5 应用程序中使用实体框架(最新版本)和 Oracle 驱动程序(最新版本)。我想在运行时为我的模型设置连接字符串,但每次我这样做时,它都会从我用来创建模型的表中接收数据。当我更改 web.config 中的连接字符串时也是如此。有人知道它为什么会这样吗?

这就是我创建连接字符串的方式:

    public static string CreateConnectionString(string userName, string service)
    {
        const string providerName = "Oracle.ManagedDataAccess.Client";

        OracleConnectionStringBuilder oraBuilder = new OracleConnectionStringBuilder();
        oraBuilder.DataSource = service + ":1521/" + service;
        oraBuilder.UserID = userName;
        oraBuilder.Password ="xxx";
        oraBuilder.PersistSecurityInfo = true;

        EntityConnectionStringBuilder efBuilder = new EntityConnectionStringBuilder();
        efBuilder.Metadata = "res://*/Model2.csdl|res://*/Model2.ssdl|res://*/Model2.msl";
        efBuilder.Provider = providerName;
        efBuilder.ProviderConnectionString = oraBuilder.ConnectionString;
        return efBuilder.ConnectionString;
    }

这是模型构造函数的重载:

公共实体(字符串连接字符串) :基础(连接字符串) {

    }

这是我的连接字符串:

【问题讨论】:

  • 请为您的上下文发布一些示例代码。
  • 这是我的连接字符串:

标签: asp.net-mvc oracle entity-framework connection-string


【解决方案1】:
     try this connection string:        
     <connectionStrings>
<add name="VoccDbContext" connectionString="metadata=res://*/Entities.Vocc.VoccModel.csdl|res://*/Entities.Vocc.VoccModel.ssdl|&#xD;&#xA;res://*/Entities.Vocc.VoccModel.msl;provider=Oracle.DataAccess.Client;provider connection string=&quot;DATA SOURCE=sameDbName;PASSWORD=somePass;USER ID=someUser;&quot;"providerName="System.Data.EntityClient" />
     </connectionStrings>

【讨论】:

  • 这看起来和我的连接字符串很像?
  • 在 oracle 客户端中检查您的 ManagedDataAccess
  • 如果我使用你的连接字符串,我会得到一个异常,说它不能将 ...DataAccess... 转换为 ManagedDataAccess
【解决方案2】:
DIRECTORY_SERVERS=(tnsnames.somesite.org:389:636)
DEFAULT_ADMIN_CONTEXT="dc=site,dc=com"
DIRECTORY_SERVER_TYPE=OID

here( LDAP.ORA file) to be modified with correct LDAP Parameters

【讨论】:

  • 这是什么意思?
  • 解释您的代码将有助于使这成为一个更好的答案。
【解决方案3】:

请勿编辑XXXModels.cs。尝试编辑XXXModels.Context.tt

public &lt;#=code.Escape(container)#&gt;()...}之后添加如下代码

public <#=code.Escape(container)#>(string connectionString)
        : base(connectionString)
    {
<#
if (!loader.IsLazyLoadingEnabled(container))
{
#>
        this.Configuration.LazyLoadingEnabled = false;
<#
}

foreach (var entitySet in container.BaseEntitySets.OfType<EntitySet>())
{
    // Note: the DbSet members are defined below such that the getter and
    // setter always have the same accessibility as the DbSet definition
    if (Accessibility.ForReadOnlyProperty(entitySet) != "public")
    {
#>
        <#=codeStringGenerator.DbSetInitializer(entitySet)#>
<#
    }
}
#>
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多