How To Validate The Entity Framework Generated Database Connection String Before Starting An Application

Introduction 

Here we going to discuss, how to validate the entity framework generated database connection string in c# before starting an application. Now we will see these in following steps 

Step 1

Once you integrate an entity framework with your application, it will add the connection string to app.config or web.config. you can see that as below
  1. <connectionStrings>  
  2.     <add name="CSharpCornerEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=SMKG;initial catalog=CSharpCorner;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />   
  3. </connectionStrings>  
now am going to make a mistake in above valid connection as below, you know that data source represents computer name or server name data source=SMKG to data source=notexist
  1. <connectionStrings>  
  2.     <add name="CSharpCornerEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=notexist;initial catalog=CSharpCorner;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />   
  3. </connectionStrings>  
Now we made an invalid connect string.

Step 2: Database.Exists Method

This Database.Exists is used to Checks whether or not the database exists on the server or local system. 

Syntax

public bool Exists() 

Example
  1. using(CSharpCornerEntities ce = new CSharpCornerEntities()) {  
  2.     try {  
  3.         bool check = ce.Database.Exists();  
  4.         Console.WriteLine(check);  
  5.     } catch (Exception ex) {  
  6.         Console.WriteLine(ex);  
  7.     }  
  8. }  
Remarks

True if the database exists; false otherwise. 

Step 3 ADO.Net - SqlConnection

Another way we will use the Sqlconnection to Checks whether or not the database exists on the server or local system. To use ConfigurationManager, we need to add a reference System.Configuration.dll in our application

Example 
  1. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CSharpCornerEntities"].ConnectionString);  
  2. try {  
  3.     con.Open();  
  4. catch (Exception ex) {  
  5.     Console.WriteLine(ex);  
  6. }  
Now run your application



You should get above error because The connection string for ADO.NET (in this case SqlConnection) doesn't take that format. You're using the one specific for Entity Framework. The ADO.NET one should be something like:
"data source=SMKG-PC\SQLSERVER;initial catalog=CSharpCorner;integrated security=True"
So, to sum it up, you need two separate connection strings, one for EF and one for ADO.NET. but without keeping separate connection string we can validate the connection string using EntityConnectionStringBuilder.

Step 4 EntityConnectionStringBuilder - ProviderConnectionString

EntityConnectionStringBuilder is a class and simple way to create and manage the contents of connection strings used by the EntityClient. We need to use the System.Data.Entity.Core.EntityClient namespace to access EntityConnectionStringBuilder. here we will see only how to get the provider connection string using EntityConnectionStringBuilder.

Example 
  1. EntityConnectionStringBuilder entityConnection = new EntityConnectionStringBuilder(ConfigurationManager.ConnectionStrings["CSharpCornerEntities"].ConnectionString);  
  2. SqlConnection connectionString = new SqlConnection(entityConnection.ProviderConnectionString);  
  3. try {  
  4.     connectionString.Open();  
  5. catch (Exception ex) {  
  6.     Console.WriteLine(ex);  
  7. }  
Now run your application, you can see that it provides ado.net connection string

 
 
Step 3 and 4 are will throw the exception if the connection is not valid.

Complete Code 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Data;  
  7. using System.Data.SqlClient;  
  8. using System.Configuration;  
  9. using System.Data.Entity.Core.EntityClient;  
  10. namespace CheckDBConnectionString {  
  11.     class Program {  
  12.         static void Main(string[] args) {  
  13.             using(CSharpCornerEntities ce = new CSharpCornerEntities()) {  
  14.                 try {  
  15.                     bool check = ce.Database.Exists();  
  16.                     Console.WriteLine(check);  
  17.                 } catch (Exception ex) {  
  18.                     Console.WriteLine(ex);  
  19.                 }  
  20.                 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CSharpCornerEntities"].ConnectionString);  
  21.                 try {  
  22.                     con.Open();  
  23.                 } catch (Exception ex) {  
  24.                     Console.WriteLine(ex);  
  25.                 }  
  26.                 EntityConnectionStringBuilder entityConnection = new EntityConnectionStringBuilder(ConfigurationManager.ConnectionStrings["CSharpCornerEntities"].ConnectionString);  
  27.                 SqlConnection connectionString = new SqlConnection(entityConnection.ProviderConnectionString);  
  28.                 try {  
  29.                     connectionString.Open();  
  30.                 } catch (Exception ex) {  
  31.                     Console.WriteLine(ex);  
  32.                 }  
  33.             }  
  34.         }  
  35.     }  
  36. }  
I hope It's helpful
Ebook Download
View all
Learn
View all