How to Deploy “SQL Server Express + EF” Application

梦想的初衷 提交于 2020-01-13 17:06:13

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/25836156/how-to-deploy-sql-server-express-ef-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!