Access VBA中的实体框架错误 - “在配置中找不到指定的命名连接...”

时间:2011-12-29 20:48:25

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

我有一个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;
    }
}
 }

1 个答案:

答案 0 :(得分:1)

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

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

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

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

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