I'm trying to set a connection string dynamically to the MyDataContext inheriting DbContext and I got this error:
No connection string named 'Data Source=myserver\\mine;Initial Catalog=MyDb;User ID=user1;Password=mypassword;Connect Timeout=300' could be found in the application config file.
Here's what my code look like:
public class DataContext : DbContext, IDataContext, IDataContextAsync
{
private readonly Guid _instanceId;
public DataContext(string nameOrConnectionString)
: base(nameOrConnectionString)
{
_instanceId = Guid.NewGuid();
Configuration.LazyLoadingEnabled = false;
Configuration.ProxyCreationEnabled = false;
}
//and so on...
}
public class MyDataContext : DataContext
{
public MyDataContext (string nameOrConnectionString)
:base(nameOrConnectionString)
{
}
}
I'm wondering why this is happening. As what I've understand, the constructor can accept either connection string name and connection string. In this case Entity Framework seem to expect a name.
Please help.
Thanks :)
You probably want DbContext(String)
and not DataContext(String)
given the EF tag (and you referencing nameOrConnectionString
argument).
public class MyDataContext : DbContext
{
public MyDataContext(String nameOrConnectionString)
: base(nameOrConnectionString)
{
}
}
Otherwise the DataContext
is looking for a file source, which could be the problem you're seeing.
尝试将连接字符串复制到MVC项目(即主项目)中的.config
文件,并确保该MVC项目作为启动。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.