关于EF连接数据库的问题

hnwl0507 2016-09-21 05:17:30
现有个问题请教下各位大侠, 问题是这样的:
采用的是Oracle 11g的数据库,在一台服务器上配置了多个实例的数据库,各用户名与密码不一致,然后用EF技术连接数据库进行相关的操作,然后通过配置更改数据库的连接字符中的不同用户与密码,可出现了问题EF操作的却总是其中一个实例所对的数据库,无法进行数据库的切换,而用PLSQL工具去连接却一切正常,查找了很久不知原因何在,请高手赐教,谢谢。


配置文件连接字符串为:
<connectionStrings>
<add name="LocalDbContext" connectionString="metadata=res://CLDC.CLAT.IQS.LocalDb.DataAccess/LocalDb.csdl|res://CLDC.CLAT.IQS.LocalDb.DataAccess/LocalDb.ssdl|res://CLDC.CLAT.IQS.LocalDb.DataAccess/LocalDb.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string="data source=test;password=clat_t3;persist security info=True;user id=CLAT_t3"" providerName="System.Data.EntityClient" />
</connectionStrings>
...全文
645 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
小数点1981 2018-01-04
  • 打赏
  • 举报
回复
是因为EF创建的ORACLE实体都限定了表前缀(用户名),我也遇到这个问题,除了重新生成模型,还不知道怎么解决
摇撼大地 2016-09-30
  • 打赏
  • 举报
回复
引用 8 楼 hnwl0507 的回复:
数据库在不同的服务器上(IP地址不一样)是没有任务问题的,就是在同一台服务器上的不同实例就会有问题使终只会读到一个实例的数据,不管怎么换连接字符串的用户名和密码都不行,查了好久网上也没有看到提到这块的问题解决方案,请高手指教,谢谢
他介绍的很详细了啊。就是用什么数据库,在DbContext类生成的时候确定了。
hnwl0507 2016-09-30
  • 打赏
  • 举报
回复
数据库在不同的服务器上(IP地址不一样)是没有任务问题的,就是在同一台服务器上的不同实例就会有问题使终只会读到一个实例的数据,不管怎么换连接字符串的用户名和密码都不行,查了好久网上也没有看到提到这块的问题解决方案,请高手指教,谢谢
hnwl0507 2016-09-30
  • 打赏
  • 举报
回复
引用 6 楼 daixf_csdn 的回复:
不是连接串问题,是你程序问题
在程序什么东西会出这问题呢,请提示下,谢谢
圣殿骑士18 2016-09-23
  • 打赏
  • 举报
回复
不是连接串问题,是你程序问题
hnwl0507 2016-09-22
  • 打赏
  • 举报
回复
引用 1 楼 daixf_csdn 的回复:
找到你程序里的LocalDbContext类,看了它的构造函数,你就知道为什么了
构造函数是这样的:

 public partial class LocalDbContext : DbContext
    {
        public LocalDbContext()
            : base("name=LocalDbContext")
        {
        }
}
    
hnwl0507 2016-09-22
  • 打赏
  • 举报
回复
采用的是 EF6 +oracle.manageddataaccess 实现的,调试跟踪连接字符串是正确的,可却读出的数据不是字符串用户所对应的实例数据库
hnwl0507 2016-09-22
  • 打赏
  • 举报
回复
@daixf_csdn 本未在程序中动态去修改,而是每次重新启动前先改了配置文件的
圣殿骑士18 2016-09-22
  • 打赏
  • 举报
回复
引用 2 楼 hnwl0507 的回复:
[quote=引用 1 楼 daixf_csdn 的回复:] 找到你程序里的LocalDbContext类,看了它的构造函数,你就知道为什么了
构造函数是这样的:

 public partial class LocalDbContext : DbContext
    {
        public LocalDbContext()
            : base("name=LocalDbContext")
        {
        }
}
    
[/quote] 还不明白吗?这句话的意思是,EF启动时,使用的配置就是配置文件中的"name=LocalDbContext"的配置,你程序中去改 是没用的。你必须用其他的构造方法代替它 看EF给你提供的几种:
public DbContext(string nameOrConnectionString);
        //
        // 摘要:
        //     Constructs a new context instance using the existing connection to connect
        //     to a database.  The connection will not be disposed when the context is disposed
        //     if contextOwnsConnection is false.
        //
        // 参数:
        //   existingConnection:
        //     An existing connection to use for the new context.
        //
        //   contextOwnsConnection:
        //     If set to true the connection is disposed when the context is disposed, otherwise
        //     the caller must dispose the connection.
        [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
        [SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public DbContext(DbConnection existingConnection, bool contextOwnsConnection);
        //
        // 摘要:
        //     Constructs a new context instance around an existing ObjectContext.
        //
        // 参数:
        //   objectContext:
        //     An existing ObjectContext to wrap with the new context.
        //
        //   dbContextOwnsObjectContext:
        //     If set to true the ObjectContext is disposed when the DbContext is disposed,
        //     otherwise the caller must dispose the connection.
        public DbContext(ObjectContext objectContext, bool dbContextOwnsObjectContext);
        //
        // 摘要:
        //     Constructs a new context instance using the given string as the name or connection
        //     string for the database to which a connection will be made, and initializes
        //     it from the given model.  See the class remarks for how this is used to create
        //     a connection.
        //
        // 参数:
        //   nameOrConnectionString:
        //     Either the database name or a connection string.
        //
        //   model:
        //     The model that will back this context.
        [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
        [SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public DbContext(string nameOrConnectionString, DbCompiledModel model);
        //
        // 摘要:
        //     Constructs a new context instance using the existing connection to connect
        //     to a database, and initializes it from the given model.  The connection will
        //     not be disposed when the context is disposed if contextOwnsConnection is
        //     false.
        //
        // 参数:
        //   existingConnection:
        //     An existing connection to use for the new context.
        //
        //   model:
        //     The model that will back this context.
        //
        //   contextOwnsConnection:
        //     If set to true the connection is disposed when the context is disposed, otherwise
        //     the caller must dispose the connection.
        [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
        [SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public DbContext(DbConnection existingConnection, DbCompiledModel model, bool contextOwnsConnection);
圣殿骑士18 2016-09-21
  • 打赏
  • 举报
回复
找到你程序里的LocalDbContext类,看了它的构造函数,你就知道为什么了

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧