|
| 1 | +# App.config |
| 2 | + |
| 3 | +By default, Rezoom.SQL creates database connections using configuration from |
| 4 | +your startup project's App.config file (or Web.config for ASP.NET websites). |
| 5 | + |
| 6 | +Note well the following: |
| 7 | + |
| 8 | +* App.config is only used at runtime. At compile time RZSQL is oblivious to this |
| 9 | + file's existence. |
| 10 | + |
| 11 | +* Conversely, [rzsql.json](Json.md) is used to configure RZSQL at compile time, |
| 12 | + and not loaded at runtime. |
| 13 | + |
| 14 | +* Only the App.config of the startup project (the exe you're running) matters. |
| 15 | + If you put an App.config in a library and reference that library from a |
| 16 | + console app, the App.config in the library makes no difference whatsoever. |
| 17 | + |
| 18 | +There are two things you need to accomplish in your App.config: |
| 19 | + |
| 20 | +1. In the `<connectionStrings>` section, add a connection string whose name |
| 21 | + matches the `ConnectionName` property from `rzsql.json`. (default: `"rzsql"`) |
| 22 | + |
| 23 | +2. In the `<system.data><DbProviderFactories>` section, add an entry for the |
| 24 | + database provider you're using. |
| 25 | + |
| 26 | +## Samples |
| 27 | + |
| 28 | +In practice, maybe you can remember the connection string parameters, but |
| 29 | +**nobody** can write a correct `DbProviderFactory` entry from scratch. The |
| 30 | +originals were found via brute force search in a Microsoft Research project, and |
| 31 | +have been copied from StackOverflow ever since. |
| 32 | + |
| 33 | +An easy way to get a working App.config as a starting point is to use the helper |
| 34 | +NuGet package for your backend, which will also install the appropriate ADO.NET |
| 35 | +provider if necessary: |
| 36 | + |
| 37 | +* [SQLite](https://www.nuget.org/packages/Rezoom.SQL.Provider.SQLite/) |
| 38 | +* [T-SQL](https://www.nuget.org/packages/Rezoom.SQL.Provider.TSQL/) |
| 39 | +* [Postgres](https://www.nuget.org/packages/Rezoom.SQL.Provider.Postgres/) |
| 40 | + |
| 41 | +If you want to set it up yourself, here are sample App.configs for each backend: |
| 42 | + |
| 43 | +### SQLite, using [System.Data.SQLite.Core](https://www.nuget.org/packages/System.Data.SQLite.Core/) |
| 44 | + |
| 45 | +```xml |
| 46 | +<?xml version="1.0" encoding="utf-8"?> |
| 47 | +<configuration> |
| 48 | + <connectionStrings> |
| 49 | + <add name="rzsql" providerName="System.Data.SQLite" connectionString="Data Source=rzsql.db" /> |
| 50 | + </connectionStrings> |
| 51 | + <system.data> |
| 52 | + <DbProviderFactories> |
| 53 | + <remove invariant="System.Data.SQLite" /> |
| 54 | + <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" /> |
| 55 | + </DbProviderFactories> |
| 56 | + </system.data> |
| 57 | +</configuration> |
| 58 | +``` |
| 59 | + |
| 60 | +### T-SQL |
| 61 | + |
| 62 | +```xml |
| 63 | +<?xml version="1.0" encoding="utf-8"?> |
| 64 | +<configuration> |
| 65 | + <connectionStrings> |
| 66 | + <add |
| 67 | + name="rzsql" |
| 68 | + providerName="System.Data.SqlClient" |
| 69 | + connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=rzsql" |
| 70 | + /> |
| 71 | + </connectionStrings> |
| 72 | + <system.data> |
| 73 | + <DbProviderFactories> |
| 74 | + <remove invariant="System.Data.SqlClient" /> |
| 75 | + <add |
| 76 | + name="SqlClient Data Provider" |
| 77 | + invariant="System.Data.SqlClient" |
| 78 | + description=".Net Framework Data Provider for SqlServer" |
| 79 | + type="System.Data.SqlClient.SqlClientFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" |
| 80 | + /> |
| 81 | + </DbProviderFactories> |
| 82 | + </system.data> |
| 83 | +</configuration> |
| 84 | +``` |
| 85 | + |
| 86 | +### Postgres, using [Npgsql](https://www.nuget.org/packages/Npgsql/) |
| 87 | + |
| 88 | +```xml |
| 89 | +<?xml version="1.0" encoding="utf-8"?> |
| 90 | +<configuration> |
| 91 | + <connectionStrings> |
| 92 | + <add name="rzsql" providerName="Npgsql" connectionString="Host=localhost;Database=rzsql;Username=your_user_here;Password=your_password_here" /> |
| 93 | + </connectionStrings> |
| 94 | + <system.data> |
| 95 | + <DbProviderFactories> |
| 96 | + <remove invariant="Npgsql" /> |
| 97 | + <add name="Npgsql Data Provider" invariant="Npgsql" support="FF" description=".Net Framework Data Provider for Postgresql" type="Npgsql.NpgsqlFactory, Npgsql" /> |
| 98 | + </DbProviderFactories> |
| 99 | + </system.data> |
| 100 | +</configuration> |
| 101 | +``` |
| 102 | + |
| 103 | +## Automatic database creation |
| 104 | + |
| 105 | +When you run migrations (via `MyModel.Migrate(MigrationConfig.Default)`), RZSQL |
| 106 | +will try to create the database described in your connection string if it does |
| 107 | +not already exist. Here's how this happens: |
| 108 | + |
| 109 | +### SQLite |
| 110 | + |
| 111 | +If the filename specified in `Data Source=somefile.db` doesn't exist, RZSQL |
| 112 | +creates an empty file by that name, which is sufficent for SQLite to "connect" |
| 113 | +to and start creating tables. |
| 114 | + |
| 115 | +### T-SQL |
| 116 | + |
| 117 | +If the connection fails for any reason other than total inability to establish |
| 118 | +communication, RZSQL attempts to reconnect with `Initial Catalog` set to |
| 119 | +`master`. If it is able to connect successfully this way, it will attempt to |
| 120 | +create the database originally named as the initial catalog, and, if successful, |
| 121 | +reconnect to that new database. |
| 122 | + |
| 123 | +[Source](https://github.com/rspeele/Rezoom.SQL/blob/master/src/Rezoom.SQL.Compiler/TSQL.MigrationBackend.fs) |
| 124 | + |
| 125 | +### Postgres |
| 126 | + |
| 127 | +If the connection fails with error "3D000" (Invalid Catalog Name), RZSQL |
| 128 | +attempts to reconnect with `Database=postgres`. If it is able to connect |
| 129 | +successfully this way, it will create the database originally named in the |
| 130 | +connection string, and, if successful, reconnect to that new database. |
| 131 | + |
| 132 | +[Source](https://github.com/rspeele/Rezoom.SQL/blob/master/src/Rezoom.SQL.Compiler/Postgres.MigrationBackend.fs) |
0 commit comments