Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First define your database settings interface for example <code>IDBConnectionSettings</code>. In the <code>app.config</code> add the connection string:</p> <pre><code> &lt;connectionStrings&gt; &lt;add name=" ConnectionString " connectionString="Integrated Security=SSPI; Persist Security Info=False; InitialCatalog=DB; Data Source=(local);" providerName="System.Data.SqlClient" /&gt; &lt;/connectionStrings&gt; </code></pre> <p>To retrieve the connection string from your <strong>Settings</strong> file or your <strong>app.config</strong> you need for example to do that:</p> <pre><code> public class DBConnectionSettings() { get ConnectionString { var connections = ConfigurationManager.ConnectionStrings; // From app.config you will get the connection string var connectionString = connections["ConnectionString"].ConnectionString; return connectionString; } } </code></pre> <p>Now you have to register the Interface somewhere in your code before using it.</p> <pre><code>unityContainer.Register&lt;IDBConnectionSettings&gt;(); </code></pre> <p>You can use it anywhere with resolve in your case.</p> <pre><code>public class MigrationDataStoreFactory : IDbContextFactory&lt;DataStore&gt; { public string _connectionString { get; set; } public MigrationDataStoreFactory(UnityContainer unityContainer) { _connectionString = unityContainer.Resolve&lt;IDBConnectionSettings&gt;().ConnectionString; } public DataStore Create() { return new DataStore(0, new DateTimeProvider(() =&gt; DateTime.Now), _connectionString); } } </code></pre> <hr> <h2>Update for default constructor</h2> <p>Make a static method or put this code in the default constructor in this way you do not have to give any params.</p> <pre><code> var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = Application.StartupPath + Path.DirectorySeparatorChar + @"app.config" }; // application name must be using (var unityContainer = new UnityContainer()) { var configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); var unitySection = (UnityConfigurationSection)configuration.GetSection("unity"); unityContainer.LoadConfiguration(unitySection, "ConnectionString"); { unityContainer.Resolve&lt;IDBConnectionSettings&gt;(); .... .... </code></pre> <p>I hope this will solve your problem! thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload