I am creating a dll project in C# using Visual Studio 2013 .Net 4.5. The project contains an entity model using EF6. My dll will be called as a plugin to an external application, that belongs to a software suite for some hardware products.
I have visual studio set up so that when I press debug it launches this external application (i will refer to as the gateway) which in turn makes calls to my dll, allowing me to debug.
The issue occurs when my dll tries to make calls to the entity model. I get:
InvalidOperationException -- An unhandled exception of type 'System.InvalidOperationException' occured in ENTITYFRAMEWORK.dll
'No connection string named 'Entity' could be found in the application config file.
Now, my config file has, very clearly, the required connection string which was added by VS when I set up the model. I have read all of the similar threads to this issue, and adding the connection string to the config is not the problem.
The gateway does have knowledge of the real DB instance and is given the connection string for that in a configuration. However, the entity connection string and the instance connection string differ, because of the entity jargon that gets added on by VS and EF.
I am able to create a console app which makes calls to my dll, capable of making the enity calls. I had to give the console app a reference to my dll and entity framework as well as add the connection string to the console apps config file.
This has led me to believe that, any app that makes calls into my dll must have knowledge of EF and the connection string.
Is this the case? If so, it would appear to me that I will be unable to use EF for this project because I will have no way to make the gateway aware of EF.
Thanks for your input!
So what I would do in this case is make a new Context which inherits from your current context and in the constructor pass in the connection string.
Alternatively you can also create a connection string that you can then pass into the constructors overload for your context.
public string GetConnectionString()
{
string connectionString = new EntityConnectionStringBuilder
{
Metadata = "res://*/Data.System.csdl|res://*/Data.System.ssdl|res://*/Data.System.msl",
Provider = "System.Data.SqlClient",
ProviderConnectionString = new SqlConnectionStringBuilder
{
InitialCatalog = ConfigurationManager.AppSettings["SystemDBName"],
DataSource = ConfigurationManager.AppSettings["SystemDBServerName"],
IntegratedSecurity = false,
UserID = ConfigurationManager.AppSettings["SystemDBUsername"],
Password = ConfigurationManager.AppSettings["SystemDBPassword"],
MultipleActiveResultSets = true,
}.ConnectionString
}.ConnectionString;
return connectionString;
}
Can't you compose the Entity connectionstring based on the sql connection string?
Otherwhise have a look at this question: How to have DLL specific config file?
Basicly you are supplying a config for the dll file. So besides of myapplication.exe.config you can have a myddl.dll.config
Related
I need to consume some .net code from classic ASP, hence I am using com interop.
This works well apart from EF6 and its connection strings. When I try and call the method that uses EF from classic ASP I get
No connection string named 'Entities' could be found in the application config file.
So I thought I would code the connection string into the context with the following:
using (Entities context = new Entities())
{
context.Database.Connection.ConnectionString = #"metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='data source=server;initial catalog=database;persist security info=True;user id=sa;password=PasswordRemoved;MultipleActiveResultSets=True;App=EntityFramework'";
However, at runtime from my .net test harness, when that method is called, we once again get:
No connection string named 'Entities' could be found in the application config file.
I made sure I specified that I would set the connection string in my code in the entity creation wizard, and subsequently the connection string isn't in my app.config file.
What is stopping EF6.1 from accepting my connection string in code rather than from the config file? Important to note I have scoured SO for solutions and none of them have stuck.
I have one solution with one project in it. This project is an asp.net mvc web application with xsockets.net websocket server (everything merged inside single project).
Everything was working for a few months, until today. Today I decided to update entity framework and xsockets.net. There were few errors on the way, but I solved almost all of them... almost.
Well, the part of my project that runs websocket server is not using correct connection string. I mean, I can login to my web application, and move around it (so asp.net mvc is using correct connection string), but my websocket server (which is using the same database) cannot gather any data from database, since it's throwing incorrect connection string exception.
And since everything is in the single project, with single web.config file, I don't know what to do next. I don't believe that this is websocket related error, maybe entity framework update has changed something? Anyways, is there any way to explicitly use connection string inside of a class? What else I can do to fix that?
When Entity Framework connects to create the entity objects it also establishes the connection string to that database. This usually isn't a problem since that project is referenced by another program that is overriding the server connection string with their own app config (or web config).
Typically in code when connecting to an instance of Sql Server, you write your code:
using ( MyServer context = new MyServer(myconfig.ConnectionStrings["MyServerName"]))
{
}
If you exclude providing he connection string when creating the instance of your context, you risk catching the default value created when you updated entity framework. So this probably answers both your questions: the why is it changed your connection string. The explicit use is the code example above.
My application is based on entity framework 5, and the connection string is dynamically generated and used in the application. This is working fine. The only problem is if I do not put connection string in app.config file, then it gives an error. That the app.config file should contain the connection string. Is there any way, I can make my process not to find connection string in app.config file. The work around is I can put dummy connection string, but i want it should not look in app.config file for connection string.
Please help. !!
Thanks in advance..
An application does not look at connection strings, it's libraries you use that do that. Luckily your question is tagged with entity-framework, so I guess you somewhere just instantiate a new DbContext(). It would be nice if you could show on what line of code the error occurs.
When you search the web for "entity framework dbcontext pass connection string" and you will find this question which links to the manual somewhere:
public DbContext(string nameOrConnectionString)
So, just supply your valid connection string to the constructor when instantiating your Entity Framework context (not its name, as that again will make it look in the application's configuration).
Thanks CodeCaster for your reply.
I am doing in this way. I have created one class which contains the static method, and in each DAL class i just call this method, and the instance for context is created with the passed connection string.
public static myDBEntities getDBContext(String connectionString) {
myDBEntities DB = new myDBEntities();
DB.Database.Connection.ConnectionString = connectionString;
return DB;
}
My application works fine. There is no issue. I mean it takes the dynamically assigned connection string, But only problem is if I remove the connection string from app.config file, then it gives me error that it expects connection string in app.config. So is there any settings or something, which can lead EDMX not to find connection string in app.config.
Your help will be appreciated. :)
I am trying to fix an app that references a .dll which contains Entity Framework code (.edmx, etc...). I do not have the source so I cannot determine the connection string the edmx file uses. When I run the app I get exceptions that the data source cannot be reached. I have the correct .mdf file that EF is "supposed" to be using, but since I cannot see the connection string, I can't verify this.
Is there another way(tool) to figure out what the connection string is for this dll?
It is quite weird that this 3rd part dll doesn't use a configuration key to get the connection string: entity framework creates a key every time you add a connection.
If the connection is hard-coded you need to use a tool like Reflector to get the connection string
http://reflector.red-gate.com/download.aspx?TreatAsUpdate=1
You can access the following property:
var connStr = Context.Connection.ConnectionString;
This will show you the connection string being used, minus any credentials. The Connection property differs slightly depending upon the version of EF you're using. In EFCF it's:
var connStr = Context.Database.Connection.ConnectionString;
I deployed my asp.net web app project that has a reference to my DAL class library. How can I change the connection string in my DAL library once deployed? The DAL code was ignoring my web.config connection string and trying to use the app.config value.
I thought I would be able to edit a config file that is associated with the class library, but I can't find one. Temporarily I edited the connection string and re-compiled and re-deployed the library.
Is there an option or way to setup the project files where it changes the values of the connection string based on being compiled in debug mode versus doing a release compile.
What is the recommended way of dealing with connection strings in web apps which reference class libraries?
Clarification:
the DAL library connection strings are also utilized by some datasets and L2S classes (.dbml) and I am not sure how to change those to reference a web.config file that sits outside the library in the web app project.
Currently Using this code to get around my L2S class problem:
public partial class MyDataContext
{
partial void OnCreated()
{
ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings["PrimaryConnectionString"];
if (cs != null)
{
this.Connection.ConnectionString = cs.ConnectionString;
}
}
}
Generally, I let the top-level project define this, via either web.config or app.config; either by specifying that the application should include a connection-string named "FOO", or (much better) allowing the calling application to pass (one of) the connection key, the connection string, or the connection to the dll.
Then you mainly just edit web.config like normal...
I usually place the string in the web.config file and let a class (possibly a global class used to reference settings in the web.config) reference it, with the following line:
class Globals
{
static string ConnectionString =
ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;
}
and use it elsewhere like:
IApartmentRepository apartmentRepository = new
ApartmentRepository(Globals.ConnectionString);
This was also Saif Khan's suggestion in his answer.
As for changing the connection strings based on compile mode, Visual Studio 2010 has support for this, see Web Deployment: Web.Config Transformation from the Visual Web Developer team blog. I do not think Visual Studio 2008 can do this out of the box, however maybe it can be done with some kind of build script.
I don't believe there is a recommended way, but a prefered way. Some store connection strings in the web.config file, while some store in the registry or machine.config, while some go to the extreme and store it remotely...yes I've seen that.
The most common storage I see and use myself is storing in the web.config. In my DAL objects I make a call to the web.config file for the connectionstring
string connStr = ConfigurationManager.ConnectionStrings["myString"].ConnectionString
as for auto changing of the connectionstring based on the app compiled mode, I've never seen that. You might have to put that check in the DAL itself to check if debug mode is turned "on" or "off". That would require 2 connectionstring entries in the web.config file.