【问题标题】:Entity Framework Error in Access VBA - "The specified named connection is either not found in the configuration..."Access VBA 中的实体框架错误-“在配置中找不到指定的命名连接...”
【发布时间】:2012-01-30 04:32:53
【问题描述】:

我有一个 Access VBA 项目,我从中引用了一个用 C# 编写的 COM Interop .TLB。此 C# 代码仅查询 SQL Server 数据库并通过简单的 LINQ-to-Entity 查询返回值。

我遇到了这个问题中提到的相同错误:

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid

但是,就我而言,它是 .ADP 应用程序中的 Access VBA,它引用我的 .Net 4.0 TLB,而不是另一个 .Net 项目。

我知道,如果它是另一个 .Net 项目,我可以在其 app.config 或 web.config 中添加 EF 连接字符串 XML。但是,如果我的“调用”应用程序是 Access 2003 VBA,解决方法是什么?

这是调用 .Net 代码的 VBA 代码

Dim CandidatePassword As String
Dim abc As New MISHash.Password

Dim PasswordStatus As Boolean
CandidatePassword = InputBox("Enter your password")
PasswordStatus = abc.IsValidPassword("myusername", CandidatePassword) ' FAILS HERE
If PasswordStatus Then
    MsgBox "Password valid."
Else
    MsgBox "Password failed."
End If

请帮忙。谢谢你。

更新:这是我的 C# 代码

using System.Linq;
using System.Runtime.InteropServices;
namespace MISHash
{

public class Password
{

   public Password()
   {

   }

   [ComVisible(true)] 
   public  void HashAndSave(string SomePassword)
    {
        string hashed = BCrypt.HashPassword(SomePassword, BCrypt.GenerateSalt(12));
        //save the hashed password in the database
    }

   [ComVisible(true)]
   public bool IsValidPassword(string CandidateUserName, string CandidatePassword)
    {

        string OriginalHashedPassword;
        using (MyDBEntities mycontext = new MyDBEntities())
        {
            OriginalHashedPassword = (from usr in mycontext.Users
                                       where usr.UserName.Equals(CandidateUserName)
                                       select usr.Password).FirstOrDefault();
        }
        bool matches = BCrypt.CheckPassword(CandidatePassword, OriginalHashedPassword);
        return matches;
    }
}
 }

【问题讨论】:

    标签: vba com .net-4.0 entity-framework-4 com-interop


    【解决方案1】:

    查看类似问题:
    Can I use / access the app.config from .net code, when called via COM

    这两个似乎是你最好的选择:

    1. 手动创建辅助 AppDomain
    2. 转换为 VSTO 项目

    编辑

    您也可以尝试在构造函数中传递一个硬编码的连接字符串:

    MyDBEntities mycontext = new MyDBEntities("Server=.\SQLEXPRESS;Database=School;Trusted_Connection=true;Integrated Security=True;MultipleActiveResultSets=True"))
    

    【讨论】:

    • 我的连接字符串如下所示: metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System .Data.SqlClient;provider connection string="Data Source=DBSERVER;Persist Security Info=True;User ID=dbuser;Password=dbpwd;MultipleActiveResultSets=True;Connect Timeout=120;Application Name=EntityFramework" 我们如何将这个传递给构造函数?
    • 我尝试了您编辑下建议的方法;我按照建议here 构建并使用了EntityConnection 连接对象,而不是连接字符串,并且它起作用了。非常感谢你给我指路。
    猜你喜欢
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多