Pergunta

How to make a connection to a database via Massive?

There is a method in Massive for opening a connection, but it accepts name of a connection string. I want to give it the connection string. How can I do this?

Foi útil?

Solução

In Massive code in Open() method is created DynamicModel and in DynamicModel constructor I found this line(Massive.cs 127 line):

ConnectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;

So that means you need to store connection string in configuration file and pass to Open method appropriated connection string name.

What you need is create overload methods to accept connection string. Here is an example:

public static DynamicModel Open(string connectionString, string providerName="System.Data.SqlClient") 
{
        dynamic dm = new DynamicModel(connectionString, providerName);
        return dm;
}

public DynamicModel(string connectionString, string providerName ="System.Data.SqlClient", string tableName = "", 
     string primaryKeyField = "", string descriptorField = "") 
{
        TableName = tableName == "" ? this.GetType().Name : tableName;
        PrimaryKeyField = string.IsNullOrEmpty(primaryKeyField) ? "ID" : primaryKeyField;
        DescriptorField = descriptorField;

        _factory = DbProviderFactories.GetFactory(providerName);
        ConnectionString = connectionString;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top