从Shell扩展读取配置文件

时间:2019-07-03 07:53:20

标签: .net dll app-config regasm sharpshell

我有一个项目设置为带有SharpShell库的shell扩展。当我使用regasm工具(带有/codebase标志)注册它时,它将一直工作到需要通过EntityNetwork使用数据库的地步。我收到此错误:

  

在此目录中找不到名为“ EntitiesName”的连接字符串。   应用程序配置文件。

当然,我在ConnectionString文件中有正确的config。看来整个config文件都没有被读取。

因此,我找到了一种破解解决方案,将其放入构造函数中:

    Dim assemblyLoc As String = [GetType].Assembly.Location
    Dim configName As String = assemblyLoc + ".config"
    AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", configName)

似乎正在读取某些内容,但不是全部,我收到一个新错误:

  

System.TypeInitializationException:“ System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发了异常。 ---> System.Configuration.ConfigurationErrorsException:为entityFramework创建配置节处理程序时发生错误:无法加载文件或程序集“ EntityFramework,Version = 6.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089”或其依赖项之一。系统找不到指定的文件。

如何使用配置文件或如何在这种设置中使用EntityFramework

这是我的config文件的样子:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <connectionStrings>
        <add name="EntitiesName" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;***&quot;" providerName="System.Data.EntityClient" />
      </connectionStrings>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
      </startup>
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
          <parameters>
            <parameter value="mssqllocaldb" />
          </parameters>
        </defaultConnectionFactory>
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
      </entityFramework>
    </configuration>

我怀疑我可能需要使用Code-based configuration。如果是这种情况,我希望在此方向上使用一些vb.net指针。

1 个答案:

答案 0 :(得分:0)

经过两天左右的疯狂搜索,一些文档(对于vb.net甚至对于c#来说几乎不存在,总是缺少某些东西),我将这两个类放在一起:

Imports System.Data.Entity
Imports System.Data.Entity.Core.EntityClient
Imports System.Data.SqlClient

Partial Public Class MyConfig
    Inherits DbConfiguration

    Public Sub New()
        Me.SetProviderServices("System.Data.SqlClient", Entity.SqlServer.SqlProviderServices.Instance)
    End Sub
End Class

Public Class ConnectionStringBuilder
    Public Shared Function Construct() As String
        Dim newConnStringBuilder = New SqlConnectionStringBuilder With {
            .UserID = "user",
            .Password = "pass",
            .InitialCatalog = "database",
            .DataSource = "server"
        }
        Dim entityConnectionBuilder = New EntityConnectionStringBuilder With {
            .Provider = "System.Data.SqlClient",
            .ProviderConnectionString = newConnStringBuilder.ToString(),
            .Metadata = "res://*/Model1.csdl|
                            res://*/Model1.ssdl|
                            res://*/Model1.msl"
        }
        Return entityConnectionBuilder.ToString()
    End Function
End Class

然后,在edmx模型类(在我的情况下为Model1.Context.vb)中,如下所示:

'------------------------------------------------------------------------------
' <auto-generated>
'     This code was generated from a template.
'
'     Manual changes to this file may cause unexpected behavior in your application.
'     Manual changes to this file will be overwritten if the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------

Imports System
Imports System.Data.Entity
Imports System.Data.Entity.Infrastructure

<DbConfigurationType(GetType(MyConfig))>
Partial Public Class MyEntities
    Inherits DbContext

    Public Sub New()
        MyBase.New(ConnectionStringBuilder.Construct())
        'MyBase.New("name=MyEntities")
    End Sub

    Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
        Throw New UnintentionalCodeFirstException()
    End Sub

    Public Overridable Property XXXs() As DbSet(Of XXX)
    'etc...

End Class

我肯定会有更好的方法,但是到目前为止,这是我所拥有的最好的方法,我将其发布在这里,以潜在地帮助和/或进一步讨论/更正。