2

Try to have a connection string during at runtime or remove it from App.config.However I am getting an error.

fyi: EF 6 CodeFirst

public class DfDbContext : DbContext
{
    public DfDbContext()
        : base(GetConnectionString())
    {

    }


    private static string GetConnectionString()
    {
        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
        builder.DataSource = "xxxxx";
        builder.InitialCatalog = "xxxx";
        builder.UserID = "xxx";
        builder.Password = "xxxx";
        builder.MultipleActiveResultSets = true;
        builder.PersistSecurityInfo = true;
        return builder.ConnectionString.ToString();

    }
}

Error

An exception of type 'System.ArgumentException' occurred in System.Data.dll but was not handled in user code

Additional information: Keyword not supported: 'initial catalog'.

Any alternative solution?

Lee
  • 768
  • 1
  • 11
  • 29
  • Data source is the name of the server. InitialCatalog is which database on that server. (For SQL Server.) – Jonathan Allen Mar 31 '16 at 06:48
  • Oh, that's probably it. From that connection string, EF has no way to know that you are talking to a SQL Server database. How do you tell EF what database type you are using so that it knows how to interpret the connection string? – Jonathan Allen Mar 31 '16 at 06:54
  • What happens if you pass in an open connection instead of a connection string? (Yea I'm guessing, but it sounds like it should work.) – Jonathan Allen Mar 31 '16 at 06:57
  • [`EntityConnectionStringBuilder`](https://msdn.microsoft.com/en-us/library/system.data.entity.core.entityclient.entityconnectionstringbuilder(v=vs.113).aspx)? – Damien_The_Unbeliever Mar 31 '16 at 07:21
  • 1
    May be add builder.Provider = "System.Data.SqlClient"; For example: https://msdn.microsoft.com/en-us/library/bb738533(v=vs.100).aspx – Denis Bubnov Mar 31 '16 at 07:53
  • MetaData is required on EntityConnectionStringBuilder – Lee Mar 31 '16 at 07:58
  • I believe there no MetaData on CodeFirst? – Lee Mar 31 '16 at 07:59

2 Answers2

0

With reference from the link. A valid connection string should be as follows

<add name="XXXX" 
     connectionString="Data Source=MONTGOMERY-XXXX;Initial Catalog=XXXX;Integrated Security=True;" 
     providerName="System.Data.SqlClient" />

I think you have missed out Provider name. Try adding it and check

Community
  • 1
  • 1
Jayaraj.K
  • 928
  • 9
  • 30
0

Problem Solved! Setting the default connection factory!

<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="Data Source=.; Integrated Security=True; MultipleActiveResultSets=True" />
  </parameters>
</defaultConnectionFactory>

With the following DbContext.

public class Model : DbContext
{
    public Model()
        : base(ConnectionString())
    {
    }

    public virtual DbSet<MyEntity> MyEntities { get; set; }

    private static string ConnectionString()
    {
        // logic here
        return "Data Source=(local);Initial Catalog=xx;Integrated Security=False;User ID=sa;Password=xx;MultipleActiveResultSets=True;App=EntityFramework";
    }
}
Lee
  • 768
  • 1
  • 11
  • 29
  • the connection string set in xml is not reading rather it is hard code in model function. can we use defaultConnectionFactory tag in app.config file ? how to read it from code ? – Monojit Sarkar Sep 09 '16 at 14:22