3

I have hard coded my connection string to the dbcontext of Entity Framework DB first.

public MirrorBranchesEntities(string connectionStringName, string db)
        : base(@"name=" + connectionStringName + " connectionString=metadata=res://*/MirrorBranches.csdl|res://*/MirrorBranches.ssdl|res://*/MirrorBranches.msl;provider=System.Data.SqlClient;provider connection string=data source=(local);initial catalog=" + db + ";user id=sa;password=Qwer0987;MultipleActiveResultSets=True;App=EntityFramework; providerName=System.Data.EntityClient")
    {
    }

There are multiple databases that uses that connection string. The problem is that it is returning an exception

System.ArgumentException: Keyword not supported: 'initial catalog'. at System.Data.Entity.Core.EntityClient.Internal.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Hashtable synonyms) at System.Data.Entity.Core.EntityClient.Internal.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms) at System.Data.Entity.Core.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString) at System.Data.Entity.Core.EntityClient.EntityConnection..ctor(String connectionString) at System.Data.Entity.Internal.LazyInternalConnection.Initialize() at System.Data.Entity.Internal.LazyInternalConnection.CreateObjectContextFromConnectionModel() at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at System.Data.Entity.Internal.Linq.InternalSet1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet1.get_InternalContext() at System.Data.Entity.Internal.Linq.InternalSet1.ActOnSet(Action action, EntityState newState, Object entity, String methodName) at System.Data.Entity.Internal.Linq.InternalSet1.Add(Object entity) at System.Data.Entity.DbSet`1.Add(TEntity entity)

Update

I have updated my connection string -- took away the double metadata attribute and removed an extra single-quote

I am trying to put the connection string in an EntityConnectionStringBuilder but could not insert it in the context.

public static MirrorBranchesEntities ConnectToSqlServer(string catalog)
    {
        var sqlBuilder = new SqlConnectionStringBuilder
        {

            DataSource = "(local)",
            InitialCatalog = catalog,
            PersistSecurityInfo = true,
            IntegratedSecurity = true,
            MultipleActiveResultSets = true,

            UserID = "sa",
            Password = "Qwer0987"
        };

        var entityConnectionStringBuilder = new EntityConnectionStringBuilder
        {
            Provider = "System.Data.EntityClient",
            ProviderConnectionString = sqlBuilder.ConnectionString,
            Metadata = "res://*/MirrorBranches.csdl|res://*/MirrorBranches.ssdl|res://*/MirrorBranches.msl", 

        };

        return new MirrorBranchesEntities(entityConnectionStringBuilder.ConnectionString, sqlBuilder.InitialCatalog);
    }

then I changed my context to base(ConnectToSqlServer(connectionStringName,db)) but it says invalid arguments.

J.P Masangcay
  • 759
  • 2
  • 10
  • 28

1 Answers1

1

Check your value. In your example there is doubled metadata= expression. Also try to use quotes inside your string:

base("name=" + connectionStringName + "connectionString='metadata=res://*/MirrorBranches.csdl|res://*/MirrorBranches.ssdl|res://*/MirrorBranches.msl;provider=System.Data.SqlClient;provider connection string=data source=(local);initial catalog=" + db + ";user id=sa;password=Qwer0987;MultipleActiveResultSets=True;App=EntityFramework'"; providerName=System.Data.EntityClient")
Andrew
  • 1,474
  • 4
  • 19
  • 27
  • Yeah, I have removed those. But it is still giving me that initial catalog error. I'll update my connection string – J.P Masangcay Dec 21 '15 at 07:24
  • what about additional quotes inside? – Andrew Dec 21 '15 at 07:25
  • Yup, took those out as well. except those for concatinations – J.P Masangcay Dec 21 '15 at 07:29
  • still without any success? – Andrew Dec 21 '15 at 07:33
  • None, I even removed "initial" from initial catalog still no success. What do you think? – J.P Masangcay Dec 21 '15 at 07:36
  • anotther thing that i found is that you trying to insert `providerName` argument inside your `connectionString` argument. is that ok for you? – Andrew Dec 21 '15 at 07:47
  • Actually, that connection string came from the generated connection string when I created the ADO connection entity framework. Since all the databases are the same, I copied the connection string and put it in the dbcontext since that is allowed. Then I made the modifications like removing the quotes and making the name and catalog into variables to be passed through. – J.P Masangcay Dec 21 '15 at 07:52
  • yes, but as I can see in my solution (which is also uses EF) in connection string node i have 3 attributes: `name, connectionString and providerName`. in you example I see that `providerName` attribute is inside your `connectionString` attribute. so in web.config we have `` – Andrew Dec 21 '15 at 07:57
  • as you can see, `connectionString` and `providerName` are separate attributes. in you string this is a single argument – Andrew Dec 21 '15 at 08:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98541/discussion-between-j-p-masangcay-and-andrew). – J.P Masangcay Dec 21 '15 at 08:17