0

This is a continuation of my past question

Basically, I am going to be connecting to multiple databases with the same table structures using entity framework DB first. I'm going to be looping and passing the database catalog through a parameter. (Database names are in a table). I am getting an invalid argument error in base(). I'm not even sure if I am doing this right because I haven't used those builder classes before. Here is my code:

public MirrorBranchesEntities(string db)
        : base(ConnectToSqlServer(db))
    {
    }

    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(sqlBuilder.InitialCatalog);
    }
Community
  • 1
  • 1
J.P Masangcay
  • 759
  • 2
  • 10
  • 28
  • I think it would be helpful if you could post the complete exception including the stack trace and the code of that base class constructor you are calling. – René Vogt Dec 21 '15 at 14:51
  • @RenéVogt It's not an exception but an error on base(ConnectToSqlServer(db)). Saying that The best overload method match for 'System.Data.Entity.Db.Context(System.Data.Entity.Infrastructure.DbCompiledModel)' has some invalid arguments when I only defined 2 parameters on ConnectToSqlServer(). – J.P Masangcay Dec 21 '15 at 15:03
  • I think we need more code. But as I see now, this code would not work anyway. If you instantiate an instance of `MirrorBranchesEntities`, you call `ConnectToSqlServer` which is defined `static` below and _returns another new instance of MirrorBranchesEntities!!_ This is an infinite loop anyway. Please fix your code before we can help you. – René Vogt Dec 21 '15 at 15:11
  • @RenéVogt I got the idea from here http://stackoverflow.com/questions/21165364/ef6-dbcontext-dynamic-connection-string. I didn't use that code in the first place. I was having trouble when I tried to put the connection string on my dbcontext (http://stackoverflow.com/questions/34390357/exception-system-argumentexception-keyword-not-supported-initial-catalog-wh). So, I'm a bit lost on answers and what to do. – J.P Masangcay Dec 21 '15 at 15:25

1 Answers1

0

The problem is probably here:

IntegratedSecurity = true,
//...
UserID = "sa",
Password = "Qwer0987"

You have to decide if you want to use IntegratedSecurity (authentication via Windows identity) or SQL Server authentication via UserID and Password.

René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • should I remove it? In my SQL SMS I'm using SQL server authentication. It said bool as the datatype so I typed true there. – J.P Masangcay Dec 21 '15 at 14:31
  • @J.PMasangcay simply set it to `false`. Removing it will probably work too, but set it explicitly to be sure. I don't know if it has a default value. – René Vogt Dec 21 '15 at 14:38
  • I tried removing it or setting it to false but I'm still getting the invalid argument error – J.P Masangcay Dec 21 '15 at 14:49