i am new to using Code First approach. Please excuse me if i am missing something very basic.
I need to pass connection string to DBcontext but i can't that connection string to app.config since i am calling a nugget in the class itself which gives me the connection string.
Class A : Dbcontext , IA
{
class A() : base ()
{
//call to nugget for giving a connection string
var connString=nugget.connString();
this.Database.Connection.ConnectionString = connString;
}
}`
is this approch correct ? i cannot test this on my local system. or should i pass the connString to base
class A() : base (connString)
A DbContext class has a constructor that you can pass your connection string while creating a new instance of it. The best approach here is to make a constructor for your Class A too and take your connection string arguments from outside, not inside your class. But if you're so sure about using having you connection string in your class, at least you should make it optional like this.
public class MyDbContext : Dbcontext , IA
{
public static readonly _defaultConnString = nugget.connString();
public class MyDbContext() : base(_defaultConnString) { }
public class MyDbContext(string connString) : base(connString) { }
}