2

Ok So I have got a bit futher with my implmentation of switching databases I have a method in my context class.

public string SetupConnections()
{

        //Specify the provider name, server and database.
string providerName = "System.Data.SqlClient";
        string serverName = ".";
        string databaseName = "DBNAME";

        // 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 = "USERNAME";
        sqlBuilder.Password = "PASS";

        // 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();
    }

But when I run the application it is saying the underlying provider failed to open for the user am I adding in the username and password correct obv disguised my true password for security reasons.

This is my orignal connection string which works. I was following this example here from technet

https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/ef/how-to-build-an-entityconnection-connection-string

And yes i no that example was intergrated security = true but I dont want to use that I want to to use username and password.

 <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=Pass;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

I am also getting this error as well I am using DB First

: The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715

I am not getting any data either i changed my entites object to as follows.

    public SMBASchedulerEntities(string sConnectionString)
        : base(sConnectionString)
    {

    }
c-sharp-and-swiftui-devni
  • 3,743
  • 4
  • 39
  • 100

1 Answers1

0

You are providing entityBuilder.Provider with "System.Data.SqlClient" but it should be "System.Data.EntityClient" to create connrction string with entity builder as per my knowledge.