C#托管COM dll在C ++非托管项目中 - 奇怪的实体框架问题

时间:2012-12-06 14:41:20

标签: c# entity-framework visual-c++ com com-interop

我正在通过COM在C ++非托管解决方案中实现C#项目。 C#项目使用EntityFramework来返回有关数据库的值。

当我们在此解决方案中从UnitTest C#项目调用“Test()”方法时,一切都很顺利。当我们从非托管C ++调用它时,在被调用的“Test()”方法中发生异常。

例外:

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

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

AServer::Server_InterfacePtr p(__uuidof(AServer::Server));
long res = p->Test(); // gives 1337, WRONG: it should give the count of users

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

    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;
        }
    }

Unittest方法:

    [TestMethod]
    public void TestMethod1()
    {
        Server server = new Server();
        var res = server.Test(); // OK: gives the count of users
    }

反正我们可以解决这个问题吗?我们考虑在“Test()”方法中将ConnectionString作为参数在模型的构造函数中使用它,但是显然Entities在它的构造函数中不接受ConnectionString作为参数。

App.Config的C#项目(包括unittest)中提供了connectionstring。 AntityFramework的使用版本是5.解决方案针对frameworkversion 4.0。

不使用EntityFramework的方法工作正常。

0 个答案:

没有答案
相关问题