Wednesday, October 29, 2014

Access Database from PowerShell Module or SnapIn using EF

PowerShell Module or Snapin is typically Class library. This means that we can't use Properties/Configuration file to store Database connection string.

If we are using EF we can use DbContext constructor like i.e.:

public class MyContext : DbContext
    {
        public MyContext ()
            : base(@"Data Source=C:\db\mydb.sdf")
        {
            Configuration.ProxyCreationEnabled = false;
            Configuration.LazyLoadingEnabled = false;          
            Database.SetInitializer(new MigrateDatabaseToLatestVersion              
                                                        <MyContext , MyContextMigrationConfiguration>());
        }
}

No comments:

Post a Comment