title: Using Entity Framework with Sql Server Compact Edition without App.Config additions
body: <p>I am developing a plug-in for Rhinoceros 6 and making editions to <code>App.Config</code> file of Rhinoceros seems impossible so far. <code>App.Config</code> of the plug-in project has no effect on App.Config of Rhinoceros.</p>
  <p>Below error message appears because I couldn't add providers and parameters to <code>App.Config</code>'s <code>&lt;entityFramework&gt;</code> section.</p>
  <blockquote>
  <p>Unable to determine the provider name for provider factory of type 'System.Data.SqlServerCe.SqlCeProviderFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.</p>
  </blockquote>
  <p>I installed <code>EntityFramework.SqlServer.Compact</code> and <code>Microsoft.SqlServer.Compact</code> with NuGet and checked for references, all seems fine.</p>
  <p>Below is my code-first dbcontext class :</p>
  <pre><code>    public class ModelLocalClipper : DbContext
      { 
          public ModelLocalClipper()
              : base(new SqlCeConnection(&quot;Data Source=&quot;+ Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+&quot;\\MyDatabase.sdf;Persist Security Info=False;&quot;),
    contextOwnsConnection: true)
          {
              Database.SetInitializer&lt;ModelLocalClipper&gt;(new CreateDatabaseIfNotExists&lt;ModelLocalClipper&gt;());
          }
          
          public DbSet&lt;Scene&gt; Scenes { get; set; }
          public DbSet&lt;LocalProject&gt; LocalProjects { get; set; }
      }
  
      public class Scene
      {
          public int SceneId { get; set; }
          public string Name { get; set; }
  
          public int LocalProjectId { get; set; }
  
          [ForeignKey(&quot;LocalProjectId&quot;)]
          public virtual LocalProject LocalProject { get; set; }
      }
  
      public class LocalProject
      {
          public int LocalProjectId { get; set; }
          public string Name { get; set; }
  
          public virtual ICollection&lt;Scene&gt; Scenes { get; set; }
      }
  </code></pre>
  <p>After searching a while I found <a href="https://stackoverflow.com/a/43688403/8712569">this solution</a> and transformed that to use for SQL Server CE as below but it didn't help either</p>
  <pre><code>public class SqlCeProviderInvariantName : IProviderInvariantName
  {
      public static readonly SqlCeProviderInvariantName Instance = new SqlCeProviderInvariantName();
  
      private SqlCeProviderInvariantName() { }
  
      public const string ProviderName = &quot;System.Data.SqlServerCe.4.0&quot;;
  
      public string Name { get { return ProviderName; } }
  }
  
  class SqlCeDbProviderFactoryResolver : IDbProviderFactoryResolver
  {
      public static readonly SqlCeDbProviderFactoryResolver Instance = new SqlCeDbProviderFactoryResolver();
  
      private SqlCeDbProviderFactoryResolver() { }
  
      public DbProviderFactory ResolveProviderFactory(DbConnection connection)
      {
          if (connection is SqlCeConnection) 
              return SqlCeProviderFactory.Instance;
  
          if (connection is EntityConnection) 
              return EntityProviderFactory.Instance;
  
          return null;
      }
  }
  
  class SqlCeDbDependencyResolver : IDbDependencyResolver
  {
          public object GetService(Type type, object key)
          {
              if (type == typeof(IProviderInvariantName)) 
                  return SqlCeProviderInvariantName.Instance;
  
              if (type == typeof(DbProviderFactory)) 
                  return SqlCeProviderFactory.Instance;
  
              if (type == typeof(IDbProviderFactoryResolver)) 
                  return SqlCeDbProviderFactoryResolver.Instance;
  
              return SqlCeProviderServices.Instance.GetService(type);
          }
  
          public IEnumerable&lt;object&gt; GetServices(Type type, object key)
          {
              var service = GetService(type, key);
              if (service != null) yield return service;
          }
      }
  
      class SqlCeDbConfiguration : DbConfiguration
      {
          public SqlCeDbConfiguration()
          {
              AddDependencyResolver(new SqlCeDbDependencyResolver());
          }
      }
  </code></pre>
  <p>Versions :</p>
  <ul>
  <li>RhinoCommon 6.30.20288.16410</li>
  <li>.NET Framework 4.8</li>
  <li>Entity Framework 6.4.4</li>
  <li>SQL Server CE 4.0</li>
  </ul>
  <p>With directives of ErikEJ, it worked! For those who want to look into the code here is the repo: <a href="https://github.com/Tahirhan/RhinoPluginSqlCECodeFirst" rel="nofollow noreferrer">https://github.com/Tahirhan/RhinoPluginSqlCECodeFirst</a></p>
  <p>Thanks!</p>