0

COM を介して C++ アンマネージ ソリューションに C# プロジェクトを実装しています。C# プロジェクトは EntityFramework を使用して、データベースに関する値を返します。

このソリューションで UnitTest C# プロジェクトから "Test()" メソッドを呼び出すと、すべてうまくいきます。アンマネージ C++ から呼び出すと、呼び出された "Test()" メソッドで例外が発生します。

例外:

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

「Test()」メソッドを呼び出すアンマネージ C++ 内の場所:

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 をパラメーターとして指定してモデルのコンストラクターで使用することを考えましたが、コンストラクターでEntitiesConnectionString をパラメーターとして受け入れないようです。

接続文字列は、C# プロジェクト (単体テストを含む) で提供されていますApp.Config。AntityFramework の使用バージョンは 5 です。ソリューションはフレームワークのバージョン 4.0 を対象としています。

EntityFramework を使用しないメソッドは正常に機能します。

4

0 に答える 0