2

How to pass ADO.NET Entity Data Model password at runtime? I can't work it out after searching for question, forum, blog. I select code-first from database which I want to connect to an existing .SDF database file.

At defining the connection and name in app.config, I have selected not to store sensitive password into config file. As I have see in some tutorial, they not advise to store password in config.

How to pass the password at runtime after SetData? I have 3 database in same folder. I have add all 3 database to EF.

Just learning how to use magnificent EF technique. Need some advise.

EF is setup in a WPF application.

App.config

<connectionStrings>
    <add name="AppUI" 
         connectionString="Data Source=|DataDirectory|AppUI.sdf" 
         providerName="System.Data.SqlServerCe.4.0" />
    <add name="AppSystem" 
         connectionString="Data Source=|DataDirectory|AppSystem.sdf" 
         providerName="System.Data.SqlServerCe.4.0" />
    <add name="AppConfig" 
         connectionString="Data Source=|DataDirectory|AppConfig.sdf"  
         providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>

I set the data directory at Application_Startup:

AppDomain.CurrentDomain.SetData("DataDirectory", GlobVars.strLogStartPath + "Database");

The EF main class

public partial class AppConfig : DbContext
{
    public AppConfig()
        : base("name=AppConfig")
    {
    }

    public AppConfig(string ConnectionString)
        : base(ConnectionString)
    {
    }

    public virtual DbSet<HardwareConfig> HardwareConfigs { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    }
}

I'm getting the value from configuration database

using (var context = new AppConfig())
{
    IQueryable<HardwareConfig> qTable = from t in context.HardwareConfigs
                                        select t;

    foreach (var t in qTable)
    {
        string devicename = t.Model;
    }
}

Currently the problem occurs at IQueryable<HardwareConfig> where it throws an exception:

The underlying provider failed on Open. The specified password does not match the database password. [ Data Source = AppConfig.sdf ]

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Luiey
  • 843
  • 2
  • 23
  • 50
  • You can store your connection string in App.Config. If you don't want your password in source code then remove the connection string from app.Config and put the connecting sting in machine.config. Each environment has its own machine.config, so people without production access cannot see your production password: https://stackoverflow.com/questions/1184187/storing-connection-strings-in-machine-config-vs-storing-them-in-web-config – Hooman Bahreini May 24 '18 at 03:40
  • @Hooman I can't see the idea. The password can be hardcoded in source code since the password will be same without changes. – Luiey May 24 '18 at 03:44
  • 1
    I guess my comment does not answer your question... I just gave you an alternative to avoid storing your sensitive password in app.config. – Hooman Bahreini May 24 '18 at 03:51
  • @Hooman I believe this alternative might be useful when I'm handling with Entity Framework for some time. Currently, I'm in the phase of getting introduction to EF. So, I cannot get into another method/alternative as I might reflect to my main intention, by getting understanding how code first in EF done. From there, I can move further with explore another features in EF. – Luiey May 24 '18 at 08:11
  • Is it asp.net app ? If yes you can put in web.config and run asp.net_regiis on the config and it will encrypt it for u – Hany Habib May 24 '18 at 08:53
  • @HanyHabib no, it's not ASP.net but WPF. Windows application – Luiey May 25 '18 at 01:23

0 Answers0