YouTip LogoYouTip

Met Websecurity Initializedatabaseconnection

## WebSecurity.InitializeDatabaseConnection() Method The `WebSecurity.InitializeDatabaseConnection()` method initializes the ASP.NET Web Pages SimpleMembership database system. It establishes a connection to the database containing user profiles and membership credentials, enabling authentication, registration, and role management features in your application. --- ## Definition This method initializes the **WebSecurity** system by connecting to a database that stores user accounts and membership information. It can also automatically generate the required membership tables if they do not already exist. --- ## Syntax ### C# Syntax ```csharp WebSecurity.InitializeDatabaseConnection( string connectionString, string userTableName, string userIdColumn, string userNameColumn, bool autoCreateTables ); ``` ### VB.NET Syntax ```vb WebSecurity.InitializeDatabaseConnection( connectionString As String, userTableName As String, userIdColumn As String, userNameColumn As String, autoCreateTables As Boolean ) ``` --- ## Parameters | Parameter | Type | Description | | :--- | :--- | :--- | | `connectionString` | **String** | The name of the connection string from your `web.config` file, or the name of the database file (excluding the `.sdf` extension if using SQL Server Compact). | | `userTableName` | **String** | The name of the database table that contains user profile information. | | `userIdColumn` | **String** | The name of the database column that contains the unique user ID (Primary Key). | | `userNameColumn` | **String** | The name of the database column that contains the user names. | | `autoCreateTables` | **Boolean** | Set to `true` to automatically create the user profile and membership tables if they do not exist; otherwise, `false`. | --- ## Return Value This method does not return any value (**void** / **Sub**). --- ## Remarks & Usage Notes * **Database Existence:** Even if `autoCreateTables` is set to `true`, the physical **database** itself must already exist. The method will only automatically create the **tables** inside that database, not the database file or instance. * **SQL Server Compact (SQL CE):** If you are using SQL Server Compact, the `connectionString` parameter should be the name of the database file without the `.sdf` extension (e.g., `"MyDatabase"` for `MyDatabase.sdf`). * **Standard SQL Server / LocalDB:** For standard SQL Server databases, the `connectionString` parameter must match the `name` attribute of a connection string defined in your application's `web.config` file. * **Initialization Placement:** This method is typically called once during application startup. In ASP.NET Web Pages (Razor), it is commonly placed inside the `_AppStart.cshtml` file to ensure it runs before any page requests are processed. > ⚠️ **Important:** If `autoCreateTables` is set to `true`, the system will automatically create the core membership tables (such as `webpages_Membership`, `webpages_OAuthMembership`, `webpages_Roles`, and `webpages_UsersInRoles`) and link them to your custom user profile table. --- ## Code Example The following example shows how to initialize the database connection in your `_AppStart.cshtml` file. ```html @* _AppStart.cshtml *@ @{ // Initialize the WebSecurity database. // This connects to the "RegisterDB" connection string defined in web.config. // It uses "UserProfile" as the user table, "UserId" as the primary key, // "Email" as the username column, and automatically creates tables if they are missing. WebSecurity.InitializeDatabaseConnection( "RegisterDB", "UserProfile", "UserId", "Email", autoCreateTables: true ); } ``` --- ## Errors and Exceptions The `InitializeDatabaseConnection()` method will throw an **InvalidOperationException** under the following conditions: * **SimpleMembership** is not initialized or has been explicitly disabled in the website configuration (`web.config`). * The method is called more than once during the application lifecycle. * The specified database connection string cannot be found or is invalid. --- ## Technical Data | Attribute | Value | | :--- | :--- | | **Namespace** | `WebMatrix.WebData` | | **Assembly** | `WebMatrix.WebData.dll` |
← Met Websecurity IsconfirmedMet Websecurity Getuserid β†’