【问题标题】:LocalDB 2012 Application not working with Framework 4.0.3 but works on 4.5LocalDB 2012 应用程序不适用于 Framework 4.0.3,但适用于 4.5
【发布时间】:2014-11-19 16:46:32
【问题描述】:

最初我使用 VS 2013 开发了一个应用程序(winforms/c#):框架 4.5、SQL Server 2012 和 EntityFramework 6,但由于用户客户端要求和场景发生变化,应用程序迁移到:框架 4.0.3, LocalDB 2012 和 EntityFramework 6。

要迁移项目框架,我只需更改目标版本,重新安装参考(包括 EntityFramework)并检查 app.config。

为了迁移 mdfs,在 VS 中,使用 SQL Server DataProvider、ServerName (LocalDB)\v11.0、WindowsAuthentication 和附加的数据库文件创建了一个数据连接,这向我显示了一个警告,它会影响数据库,然后创建连接,此连接用于创建 ADO 实体数据模型。

为了在客户端上部署,我使用了 WIX 项目。

当我在安装了 Framework 4.0.3 的机器上部署时,应用程序崩溃并出现以下错误:

"Underlying provider failed on open" and the innerexception was "A network-related or instance-specific occurred while establishing ..."

using (var dbContext = new SEDGRAICEntities())  <<-- Crashes Here
{                            
    try
    {
        var baseubigeo = (from u in dbContext.T_MAE_UBIGEOCCPP
                          select u.id_departamento.Trim() + u.id_provincia.Trim() + u.id_distrito.Trim() + u.id_centropoblado.Trim()).ToList();

        var query = from x in baselocalcp
                    where !baseubigeo.Contains(x)
                    select x;

        result = query.AsEnumerable<string>().ToList();
    }
    catch (DbEntityValidationException dbEEx)
    {
        dbContext.Database.Connection.Close();
        dbContext.Dispose();
        throw dbEEx;
    }
    catch (Exception Ex)
    {
        dbContext.Database.Connection.Close();
        dbContext.Dispose();
        throw Ex;
    }


}

SEDGRAIC实体:

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

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public virtual DbSet<DIS2K06> DIS2K06 { get; set; }
    public virtual DbSet<T_MAE_UBIGEOCCPP> T_MAE_UBIGEOCCPP { get; set; }
    public virtual DbSet<T_SEDD_GEOLOCALIZACIONCCPP> T_SEDD_GEOLOCALIZACIONCCPP { get; set; }

    public virtual ObjectResult<UbigeoValidacion> sp_ValidaUbigeoWGS84(string p_ver, string p_xmlData, Nullable<bool> p_boolGeo)
    {
        var p_verParameter = p_ver != null ?
            new ObjectParameter("p_ver", p_ver) :
            new ObjectParameter("p_ver", typeof(string));

        var p_xmlDataParameter = p_xmlData != null ?
            new ObjectParameter("p_xmlData", p_xmlData) :
            new ObjectParameter("p_xmlData", typeof(string));

        var p_boolGeoParameter = p_boolGeo.HasValue ?
            new ObjectParameter("p_boolGeo", p_boolGeo) :
            new ObjectParameter("p_boolGeo", typeof(bool));

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<UbigeoValidacion>("sp_ValidaUbigeoWGS84", p_verParameter, p_xmlDataParameter, p_boolGeoParameter);
    }
}

App.Config 上的 SEDGRAICEntities 连接字符串是:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <appSettings>
    <add key="BusinessID" value="20102010201" />
    <add key="BusinessName" value="TESTBUSINESS" />
  </appSettings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
  </startup>
  <connectionStrings>
    <add name="SEDGRAICCUADROSEntities" connectionString="metadata=res://*/erCUADROSmodel.csdl|res://*/erCUADROSmodel.ssdl|res://*/erCUADROSmodel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\SEDGRAIC.mdf;integrated security=true;connect timeout=60;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="SEDGRAICEntities" connectionString="metadata=res://*/erSEDGRAICmodel.csdl|res://*/erSEDGRAICmodel.ssdl|res://*/erSEDGRAICmodel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\SEDGRAIC.mdf;integrated security=true;connect timeout=60;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

问题是当我在具有框架 4.5 的机器上部署完全相同的安装程序时,该应用程序运行良好。

我使用 VS 2010 和框架 4.0.3 重新创建了解决方案,结果相同。

要求维护framework 4.0.3,以保持与客户端机器的兼容性,所以我不能使用framework 4.5。

如果我删除所有(我认为)依赖项甚至重新创建解决方案,我不明白为什么应用程序需要框架 4.5 才能工作。

请帮帮我。

奥马尔

【问题讨论】:

  • 客户端机器是否安装了localdb?
  • 是的,客户端安装了LocalDB msi文件
  • 并且v11.0的实例是否存在——可以使用sqllocaldb.exe来查看
  • 我使用了该工具(sqllocaldb.exe),创建了一个实例,连接到它以及所有...所有这些都在客户端计算机上...
  • 我觉得这个方法还不够:stackoverflow.com/questions/8065002/…

标签: c# .net winforms entity-framework localdb


【解决方案1】:

感谢 ErikEJ,我找到了解决方案:

framework 4 的独立安装程序不包含支持 LocalDB 所需的更新(即使注册表 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full] 上的版本显示为 4.0.30319) 基本上框架需要安装4.0.2版本的框架。认为我们有更好的版本可能会产生误导,但实际上我们没有。

因此,感谢 ErikEJ,我们发现我们有一种更有效的方法来找到正确的版本,实际上如下所述:How can I find out if .NET 4.02 is installed?。 我只有 2 个注册表项,而不是我们需要的总共 6 个。

所以我们必须安装更新到 4.0.2 (KB2544514) 才能获得它们。安装后,现在我们有了安全使用 LocalDB 数据库所需的 6 个密钥。

奥马尔

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 2019-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    相关资源
    最近更新 更多