I have an ASP.NET MVC 5 application. And I use Entity Framework 6 Code First in order to access a database in another machine.
I use the connection string from the DbContext to make another direct sql query to the database, using a micro ORM. In order to get the connection string I use:
myContext.Database.Connection.ConnectionString
The problem is that inside an Action, the connection string changes after a call to the method "Find" on a context DbSet. Previously, the database password is there, but after calling this method, the password just goes away.
Has anybody experienced such change to the connection string?
Thank's in advance.
This is per design. See https://connect.microsoft.com/VisualStudio/feedback/details/514829/datacontext-getcommand-alters-the-contexts-connection-string
You have to save the connection string before it has opened.
Related
Recently I am working on an .Net project. We used EF to handle SQL, when we make an installer of the program, we realize that app.config is visible which mean that the connection string is not safe.
I am looking for a way to add connection string (or maybe secret code and username) to the EF so that the connection string is not visible.
Something like change old code from this
Using db As ConnectDb.adoSentoEntities= New ConnectDb.adoSentoEntities
'TODO
End Using
to this
Using db As ConnectDb.adoSentoEntities= New ConnectDb.adoSentoEntities(ConnectionString)
'TODO
End Using
But since we used connect code to SQL all over the place, changing every single line of code is not possible. There is a way I only need to add connection string once?
You’d be better off encrypting the connection string section in the app.config. You wouldn’t need to make any changes.
Storing any sort of configuration in an assembly can be read using a hex editor.
It’s been answered on here before.
Encrypting Connection String in web.config
You’d be better off using a trusted connection if you’re using SQL Server. The user running the app would need to have permissions and no username and password is required.
Save connection string is settings of project properties.
Go in project properties.
Select settings.
Add new setting as connection string and save connection string.
Then you can use it for whole project.
I have a database where I created a contained user and I needed to connect to my web app using that user. I have always been able to connect to the web app with a standard user having Persist Security Info=False.
However, the only way I was able to connect with the contained user was changing my connection string to Persist Security Info=True, otherwise I'd get a login failed sql exception even though I was able to connect using SSMS. I'm not sure why it worked, does anybody know why a contained user needs the property set to True?
For you web app, are you using Entity Framework ?
And for your DbContext are you using IdentityDbContext ?
If so, I had the same problem. I was able to connect directly with SqlConnection but encountered an "Access Deny" error when connecting with Entity Framework.
When I gave enough permissions to my user, all queries were very slow.
When instantiating the Context (with IdentityDbContext) you should set the second parameter to false.
public AdeleDbContext(string connectionString) : base(connectionString, false)
{
}
The second parameter is throwIfV1Schema and when set to true (which is the default value), it will validate schema against the database by calling SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME=#Table for many columns.
That was the reason why the connection was slow and user needed more permissions when connecting to DB with Entity Framework and IdentityDbContext.
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.
I am trying to use nHibernate and Castle and make a connection to one database based on the connection string stored in another database. Is there any way to change the connection string for SessionManager dynamically?
the connectionstring is baked into the sessionfactory. i doubt that you can change it. you could create a lightweight factory to get the connection string or just use ADO.NET directly and then build up the real factory.
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;