Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception: System.ArgumentException: Keyword not supported: 'initial catalog' when hard coding connection string to dbcontext

Tags:

c#

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.

like image 304
J.P Masangcay Avatar asked Jan 28 '26 17:01

J.P Masangcay


1 Answers

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")
like image 132
Andrew Avatar answered Jan 31 '26 05:01

Andrew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!