locked
Porgrammable Connection RRS feed

  • Question

  • User-1868255333 posted

    I am trying to setup my connection to entity via code as I have to give the user the option to change database with same schema. Now it seems to build the connection string fine but it throws the following error

            public string SetupConnections()
            {
    
    
    
                //Specify the provider name, server and database.
    string providerName = "System.Data.EntityClient";
                string serverName = "sourcenet";
                string databaseName = "COASTALCAROLINAPODIATRY";
    
                // Initialize the connection string builder for the
                // underlying provider.
                SqlConnectionStringBuilder sqlBuilder =
                    new SqlConnectionStringBuilder();
    
                // Set the properties for the data source.
                sqlBuilder.DataSource = serverName;
                sqlBuilder.InitialCatalog = databaseName;
                sqlBuilder.IntegratedSecurity   = false;
                sqlBuilder.UserID = "";
                sqlBuilder.Password = "";
                    
                // Build the SqlConnection connection string.
                string providerString = sqlBuilder.ToString();
    
                // Initialize the EntityConnectionStringBuilder.
                EntityConnectionStringBuilder entityBuilder =
                    new EntityConnectionStringBuilder();
    
                //Set the provider name.
                entityBuilder.Provider = providerName;
    
                // Set the provider-specific connection string.
                entityBuilder.ProviderConnectionString = providerString;
                
                // Set the Metadata location.
                entityBuilder.Metadata = @"res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl";
                Console.WriteLine(entityBuilder.ToString());
    
                using (EntityConnection conn =
                    new EntityConnection(entityBuilder.ToString()))
                {
                    conn.Open();
                    Console.WriteLine("Just testing the connection.");
                    conn.Close();
                }
                return sqlBuilder.ToString();
            }
            public void AddToPatient(Patient newPatient)
            {
                using (var myContext = new SMBASchedulerEntities(this.Connectionstring))
                {
                    myContext.Patients.Add(newPatient);
                    try
                    {
                        myContext.SaveChanges();
                    }
    
                    catch (DbEntityValidationException ex)
                    {
                        foreach (var entityValidationErrors in ex.EntityValidationErrors)
                        {
                            foreach (var validationError in entityValidationErrors.ValidationErrors)
                            {
                                Console.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
                            }
                        }
    
                    }
                }
            }
    
    

    But i am getting the following error 

    An unhandled exception of type 'System.ArgumentException' occurred in EntityFramework.dll

    Additional information: The ADO.NET provider with invariant name 'System.Data.EntityClient' is either not registered in the machine or application config file, or could not be loaded. See the inner exception for details.

    Even thoguh my app config uses entity.frame

      <add name="SMBASchedulerEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=sourcenet;initial catalog=COASTALCAROLINAPODIATRY;User ID=scheduler;Password=removed for security;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

     

    Tuesday, January 2, 2018 12:57 PM

All replies

  • User753101303 posted

    Hi,

    I would debug this by just showing the builded connection string to compare with one found in a config file.I would try tro use the connection string only once I have seen it is built correctly.

    For now I guess you perhaps miss a "provider=System.Data.SqlClient" in the inner connection string and/or that you don't hjave a reference to System.Data.Entity ?

    Also rather than rebuilding from scratch, my approach would be likely to load a usual connection string that works from the web.config and to just change the exact parts which needs to be changed.

    Tuesday, January 2, 2018 1:14 PM
  • User1120430333 posted

    I have seen this done with a user.config that held the connectionstring. Powershell script was used to change the connectionstring, which happened at the Command Prompt.

    Tuesday, January 2, 2018 1:20 PM
  • User-832373396 posted

    Hi <g class="gr_ gr_5 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="5" data-gr-id="5">david</g>,

    Sir, how about re-designing the solution?

    To keep multiple databases at the same time;

    For example:

    <add name="SMB1ASchedulerEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=sourcenet;
    initial catalog=COASTALCAROLINAPODIATRY1;User ID=scheduler;Password=removed for security;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> <add name="SMB2ASchedulerEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=sourcenet;
    initial catalog=COASTALCAROLINAPODIATRY2;User ID=scheduler;Password=removed for security;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

     Then,

    In code:

    SMBA1SchedulerEntities myContext1 = new SMBA1SchedulerEntities();
    SMBA2SchedulerEntities myContext2 = new SMBA2SchedulerEntities();
    DbContext myContext;
     
    if(user want myContext2 ){
    myContext=myContext2 ;
    
    }
    else{
       myContext=myContext1 ;
    }

    Usage:

    List<APerson> c= myContext.Set<APerson>().ToList();

    With regards, Angelina Jolie

    Thursday, January 4, 2018 9:35 AM