C+中的C#托管COM dll+;非托管项目-奇怪的实体框架问题 我通过COM在C++非托管解决方案中实现了C项目。C#项目使用EntityFramework返回有关数据库的值

C+中的C#托管COM dll+;非托管项目-奇怪的实体框架问题 我通过COM在C++非托管解决方案中实现了C项目。C#项目使用EntityFramework返回有关数据库的值,c#,entity-framework,visual-c++,com,com-interop,C#,Entity Framework,Visual C++,Com,Com Interop,在这个解决方案中,当我们从UnitTest C#项目调用“Test()”方法时,一切都很顺利。当我们从非托管C++调用它时,在调用的“测试()”方法中会出现异常。 例外情况: No connection string named 'Entities' could be found in the application config file. 非托管C++中的位置,我们称之为“测试()”方法: C#项目中被调用的方法: 我们能解决这个问题吗?我们曾考虑在“Test()”方法中将Connect

在这个解决方案中,当我们从UnitTest C#项目调用“Test()”方法时,一切都很顺利。当我们从非托管C++调用它时,在调用的“测试()”方法中会出现异常。

例外情况:

No connection string named 'Entities' could be found in the application 
config file.

非托管C++中的位置,我们称之为“测试()”方法:

C#项目中被调用的方法:

我们能解决这个问题吗?我们曾考虑在“Test()”方法中将ConnectionString作为参数,以便在模型的构造函数中使用它,但显然
实体
不接受ConnectionString作为其构造函数中的参数

connectionstring已在
App.Config
中的C#projects(包括unittest)中提供。反框架的使用版本是5。该解决方案以FrameworkVersion4.0为目标


不使用EntityFramework的方法工作得很好。

将连接字符串配置部分放在测试项目中!!失败的不是测试项目。就像我说的“connectionstring已经在App.Config中的C#项目(包括unittest)中提供了。”。是非托管项目获取了错误的值。不,这不起作用:VisualStudio显式给出了一个不可能的错误,它只有一个defaultconstructor。就像我说的:“但实体显然不接受ConnectionString作为其构造函数中的参数。”。这在EntityFramework 4.1版中有效,但在5版中无效。您应该看到应用程序使用的配置文件是什么,这是一个问题和解决方案:它给了我一些见解,但没有实际使用的信息。我认为,因为unittest有一个配置,所以定位connectionstring没有问题。非托管C++项目没有这样的系统,所以我需要在那里找到解决方案。
AServer::Server_InterfacePtr p(__uuidof(AServer::Server));
long res = p->Test(); // gives 1337, WRONG: it should give the count of users
    public int Test()
    {
        TextWriterTraceListener myTextListener = new TextWriterTraceListener(@"C:\log.txt");
        Trace.Listeners.Add(myTextListener);
        try
        {
            Entities model = new Entities();
            ObjectResult<UserGetAll_Result> res = model.UserGetAll();
            return res.Count;
        }
        catch (Exception e)
        {
            Trace.WriteLine(e.Message); // writes out "No connection string named 'Entities' could be found in the application config file."
            return 1337;
        }
    }
    [TestMethod]
    public void TestMethod1()
    {
        Server server = new Server();
        var res = server.Test(); // OK: gives the count of users
    }