How do I connect to a remote database using ASP.NET MVC?
I have an ASP.NET MVC application running using a Sever express 2005 SQL database instance running on my development machine. However, when the application is deployed to production, the IIS instance and the SQL Server instance will be on different logical machines. I don't know if they are different physical boxes, but I doubt it.
Currently, the production database server is in place with a populated database. Since I have read access to this instance, I would like to point the project on my dev machine to make sure the remote DB connection is still working. Obviously, the schema is identical and the layout and content are different.
I currently have a project linked to a .mdf file located on my development machine. What changes do I need to make to switch it to the DB instance on the remote server?
source to share
Easy answer: change the connection string for your data-tier (Linq to SQL?) In web.config to point to an out-of-process database instance.
In more detail, here are the steps I took to split the database on my site in my development box. By doing the following, you can check the configuration of the two servers locally before deploying it to production.
- Install SQL Server Management Studio
- Open Management Studio and connect to local SQL Server Express localization (typically [boxname] / SQLEXPRESS)
- Right-click on the Databases folder on the management server and select Attach ...
- Find your .mdf file in your project and open it.
- Rename the newly attached database to whatever you want it to be called.
- Grant any service account whose IIS application pool runs under database permissions. First, create an account under the top-level Security / Logins folder, and then add that account to the new Security / Users folder. I am using NETWORK SERVICE for my configuration.
Now that your database is mounted as a separate database in SQL Server Express, you can connect to the database using a normal SQL Server connection string .
At this point, your development environment simulates a production environment where databases and IIS process are broken. You only need to change the connection string during production to point to the production database.
source to share