Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Deploy "SQL Server Express + EF" Application

It's my first time to Deploy an Application which uses SQL Server Express Database. I'm Using Entity Framework Model First to contact Database. and i created a Setup wizard with Install Shield to Install the App.

These are Steps that I'v done to Install The Application in Destination Computer :

  1. Installing MS SQL Server Express (DEST)
  2. Installing The Program using Setup file (DEST)
  3. Detach Database from SQL server and Copy Related .mdf ,.ldf file to the Destination Computer.
  4. Attach database file in destination computer using SQL Server Management Studio.

I know server names and SQL name Instances are different and my program can't run correctly with the Old Connection String.

I'm Beginner at this, and I want to know what should I do in the Destination Computer to make the program run?

  • should I find a way to change the connection string on runtime?!
  • or is there any way to modify installshield project and it do this work for me? (installshield is professional edition)
  • could you suggest me what to do?

in my searches I saw that WiX can do this, but I find it complicated, and i don't have enough time to learn it. i need to deploy the app ASAP.

Thanks alot.

like image 869
Mahdi Rashidi Avatar asked Sep 14 '14 17:09

Mahdi Rashidi


1 Answers

Few hints for using LocalDB in your project:

  1. Download SQL Express LocalDB 2014 here. You can install it silently with single command like this

    msiexec /i SqlLocalDB.msi /qn IACCEPTSQLLOCALDBLICENSETERMS=YES

  2. Include your .MDF in your VS project and set in properties to Copy if newer so that it gets copied to your bin folder during build so that it is automatically included in the installer.

  3. At your app startup (in app.cs) check, if database file exists in desired location (e.g. %PUBLIC%\YourApp\Data) (WPF Desktop Application with MDF used locally for all local users). If not, copy the .mdf file from your app install dir to your data dir.

  4. Modify app.config so that your connection string looks like: <add name="MyContextLocalDB" connectionString="Server=(localdb)\MSSQLLocalDB; Integrated Security=True; AttachDBFilename=|DataDirectory|\MyDatabase.mdf; Connection Timeout = 30" providerName="System.Data.SqlClient" />

Connection timeout should be increased, since LocalDB exe is launched when you first try to connect to it.

You can also use Database.CreateIfNotExists, but I have never tried it.

like image 142
Vojtěch Dohnal Avatar answered Nov 17 '22 04:11

Vojtěch Dohnal