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.InternalSet
1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet
1.get_InternalContext() at System.Data.Entity.Internal.Linq.InternalSet1.ActOnSet(Action action, EntityState newState, Object entity, String methodName) at System.Data.Entity.Internal.Linq.InternalSet
1.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.