Set Up the Database Connection for a Deployed Application
- 4 minutes to read
This lesson explains how to connect your WinForms and Blazor XAF application to a database server, create an initial database, and resolve database and application version mismatches. For other cases, for instance, non-Windows deployment to Azure AppService or Linux, see topics from the following help section: Deployment Recommendations for XAF ASP.NET Core Applications (Blazor Server, Middle Tier Server, or Web API Service).
- Run the application at the target workstation or server. If the application displays an error message, refer to the eXpressAppFramework.log file located in the application folder for details. The log should contain the following message: “The application cannot connect to the specified database, because the database doesn’t exist, its version is older than that of the application or its schema does not match the ORM data model structure.“ If you see another message, review the lesson you followed to deploy the application.
- Ensure that your database server is up and running. The database server must also accept remote connections. If your application uses Middle Tier Server, it must be run.
Open the application configuration file. It is an JSON or XML file located in the application folder.
- MySolution.Blazor.Server\appsettings.json for Blazor/WebAPI applications.
- MySolution.Win.exe.config or MySolution.Win.dll.config for WinForms applications.
Note
The following code samples demonstrate an approach for Microsoft SQL Server. Modify the code as needed for your database provider. For additional information, refer to the following help topic: Specify EF Core Database Provider in XAF Application.
"ConnectionString": "Integrated Security=SSPI;Pooling=true;MultipleActiveResultSets=true; Data Source=(localdb)\\mssqllocaldb;Initial Catalog=MySolution",Substitute
(localdb)\mssqllocaldbwith the database server name or its IP address. Uselocalhostor(local)if you use a local database server.If your database server authentication mode is SQL Server Authentication, disable integrated security and add user credentials.
"ConnectionString": "Integrated Security=False;Pooling=true;MultipleActiveResultSets=true; Data Source=YOUR_DB_SERVER;Initial Catalog=MySolution;User ID=YOUR_USER_ID;Password=YOUR_PASSWORD",An account used to run the application must have appropriate permissions at the database server. You can use an administrative account for training purposes and set up a limited account later (see Database Security References lesson).
Launch a command line interpreter, for instance, Command Prompt. Run your application with the
-updateDatabasecommand and follow the on-screen instructions. The update mechanism connects to the database server, determines that the application database does not yet exist, and creates it. If the database exists, the mechanism updates the database structure.Refer to the following topic for additional details: Production Database and Application Updates.
Run the application and ensure that the database is successfully created or updated.

Your local database on the Developer Workstation may contain objects created while developing and debugging an application. If you want this data to be accessible to users, create a database backup on the Developer Workstation and restore it on the database server with Microsoft SQL Server Management Studio. Connect to a local database server at the Developer Workstation. Right-click your database name in Object Explorer and choose Tasks | Back Up… | Database…. Select a backup destination in the invoked dialog and click OK. Connect to the database server. Right-click your database name in Object Explorer and choose Tasks | Restore… | Database…. Select the backup file in the invoked dialog and click OK. Run the application to view the objects created when developing and debugging the application.

Note
If you made changes to the application solution after deployment, you will get an error, stating a database and application version mismatch: “An error with number 1111 has occurred. Error message: The database version is greater than the application version. The application needs to be updated. Please contact your system administrator or download a new version.”. In this instance, update the application as described in the Application Update lesson.
Follow the Database Security References lesson recommendations to make your database more secure.