I have 2 projects in the solution - one class library with entities for database (using EF), one for Winforms.
Connection string is in app.config of the Winforms application, and it works correctly:
<add name="LibraryContext"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\MyLibrary.mdf;Integrated Security=True; "
providerName="System.Data.SqlClient"/>
But when I try to provide migrations, I get an error:
A file activation error occurred. The physical file name '\MyLibrary.mdf' may be incorrect. Diagnose and correct additional errors, and retry the operation.
When I provide full path in connection string, everything works fine. I assume the problem is in DataDirectory. The project with entities for database doesn't see it or maybe see it as own DataDirectory and not the one for startup project (Winforms).
Please, help me to provide correct relative path for database.
Try this hope this will works.
Go to Property of project check setting tab. If you contain more then one connection string then remove it. and add connection string which you have in app config.
Thanks.
Happy Coding :)
Related
I recently finished building this C# Winforms app that uses a localDB connection and when installing it on another PC, I get the error as shown in the title, even though I included the SQL Server Express localDB in the prerequisites folder.
I tried all kinds of solutions like changing the method of how to build the setup file (using Advanced Installer), but it seems that the problem is always related to the constant AttachDbFilename attribute of the connection string that's not changing according to where the database is newly installed.
Here's how the connection string is defined in the App.config file:
<connectionStrings>
<add name="client"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Surface\source\repos\VisaTurbo\VisaTurbo\Client.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Please I've been stuck with this problem for 3 days now and the due date to submit the application have already been reached.
Thanks in advance
You can by example check the database file exist in %userProfile%\DBName.mdf and create a new one with https://github.com/martincostello/sqllocaldb.
But this make the connectionstrings parameters obsolete :)
You can also generate a new one with SqlConnectionStringBuilder
But if the user move the database file this will reset the application each time.
That's why in general the database in on a server, not locally stored on the user's machine.
I'm having a issue with ConfigurationManager not picking up a change to a connection string.
I'm sure there's a simple explanation, however I've been staring at it for the last two hours and given myself contextual blindness.
Inside a WCF project I've updated the database name of a named connection string in the web.config:
<connectionStrings>
<add name="appDb"
connectionString="Data Source=HOST;Database=DB_NEW;User=X;Password=X;"
providerName="System.Data.SqlClient" />
</connectionStrings>
There's a separate repository project that orchestrates the database connection, abstracting it from the WCF project. In that project it uses the connection string value from the web.config file, accessed using the following expression:
ConfigurationManager.ConnectionStrings["appDb"].ConnectionString
(The WCF project manages dependency injection using Unity and is responsible for creating an instance of the session factory in the referenced project for NHibernate's ISessionFactory.)
However, at runtime it's still picking up the connection string prior to me changing it.
Things I've tried:
Full clean/rebuild of the entire solution
iisreset
Checked IIS physical path is the correct directory (I have multiple branches of the same project)
Checked the connection string isn't being defined anywhere else in the solution
Restarted Visual Studio (2015)
No transforms being applied to the web.config
The connection string value must be being cached somewhere, but where I do not know.
Update
Could this be NHibernate caching the connection string? Apologies I have very little knowledge with NHibernate (I'm usually EF).
var config = new NHibernate.Cfg.Configuration()
.SetProperty(Environment.ConnectionString,
ConfigurationManager
.ConnectionStrings["AppDb"].ConnectionString)
Update
IIS seems to recognise the new config in IIS Manager, so this confirms that it's being read from the file correctly.
i downloaded a demo application from http://code.msdn.microsoft.com/Authorization-based-aa2c0185/view/SourceCode
I run it on my Visual Studio and all worked fine. There is a database ASPNETDB.MDF in its App_Data folder which is used to store the user information or retrieve information for login purpose.
Now what I want is to change this database with another database in my SQL Server.
For that there can be two ways :
Locate the connection string etc. for ASPNETDB and change them with my own database.
or add this ASPNETDB file to SQL Server by attaching. But its showing error while attaching it.
Also if I exclude App_Data folder from project...Then even it works...
Where can find connection string for DB? I want to change the DB with mine own ?
Please Help
Thanks
It's using default connection string from machine.config:
<add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
Go to the Web.debug.config file. There you will find comments noting how to wire up your config file. This will use config transforms so that when you build in debug mode, it will change your config file to override the machine defaults (which is what it is currently using). Just follow the comments and you should be good :)
I have an ASP.NET MVC project that uses a SQL Server Compact database. I have the following connection string for my FoobarContext:
<add name="FoobarContext"
connectionString="Data Source=|DataDirectory|Foobar.sdf"
providerName="System.Data.SqlServerCe.4.0"/>
I fired up the project and it appears to persist data as expected. However, I can't find a file named 'Foobar.sdf' anywhere. What am I missing?
Update:
It wasn't taking my connection string. Setting a breakpoint and inspecting the context, I found its ConnectionString was actually the following:
Data Source=.\\SQLEXPRESS;Initial Catalog=MvcApplication3.Infrastructure.Data.FoobarContext;Integrated Security=True;MultipleActiveResultSets=True
Mystery solved.
It definitely should be ~/App_Data by default, but you can try running AppDomain.CurrentDomain.GetData("DataDirectory") to see where ASP.Net thinks the directory exists.
If this reveals nothing of interest, you may want to make sure your windows explorer is set to show hidden files, just in case that's why you don't see the file.
It should be in a folder called App_Data off the root of your MVC project.
you should be stingy on the app folder in your project, the database file you'll find inside.
Regards
I have a web application which i make on my local host and publish it on different servers.
in the web config of this application i have connectionstrings property like:
<connectionStrings>
<add name="MyConnectionString" connectionString="Data Source=XYZ-PC\SQLEXPRESS;Initial Catalog=SumooHServerDB;Integrated Security=True" providerName="System.Data.SqlClient"/>
Now connectionstring data source has the name of my server and when ever i publish it and run this application on different server i have to change XYZ-PC\SQLEXPRESS to the name of the server..
Is there a way i dont have to do this as it does not feel right..
any suggestions..
thanks
Try replacing XYX-PC with localhost provided the instance name is the same.
If database stands on the same server as IIS, you can use Data Source=localhost\sqlexpress
I like to use configSource to pull the connection string out into a separate file, as explained here*: http://stevenharman.net/blog/archive/2007/06/07/tip-put-connection-strings-in-their-own-configuration-file.aspx
That way, you can configure each server's connectionStrings.config once, but continue updating their web.config files with a single version that works for all of them.
* Except, I usually name it connectionStrings.config, so it's more obvious for maintenance by others.
The following article could be a solution to your question :
Handling Multiple Environment Configurations with .NET 2.0
Basically, the idea is to use the fact that in your config file you can indicate that some sections have to be read from an external file. Than during the build of your project you copy the right external file according to your environment. I think the link will explain this better.
Take a look at Web Deployment projects.
In addition to letting you merge everything into a single assembly, it give you the option of doing section-based web.config replacements. We use this on our build server to alter the standard web.config for running in the test environment. As a bonus, you're not limited to just changing connection strings. Anything in the web.config is fair game.
Use this connection string:
name="MyConnectionString" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=SumooHServerDB;Integrated Security=True" providerName="System.Data.SqlClient"
It will work.