0

I am creating 2 projects that have the same database (it's an MDF database). The first one is the map editor, and I use XNA 4 and Web Services to connect to it. The second one is the game itself and uses XNA 3.1 and Entity Data Model to connect database.

When I run the map editor and access the database, it runs properly. Bbut when I run the game and access the database, it shows an error "The underlying provider failed on Open"

I think the connection from the web service is not closed yet. But I don't know where I should close the connection.

Here is my code from the web service:

public Map AddNewMap(string username, string mapName, int sizeX, int sizeY)
    {
        using (BaseModelDataContext context = new BaseModelDataContext())
        {
            Map newMap = new Map()
            {
                Username = username,
                Name = mapName,
                SizeX = sizeX,
                SizeY = sizeY,
                Upload_Date = DateTime.Now,
                Status = 0
            };

            context.Maps.InsertOnSubmit(newMap);
            context.SubmitChanges(System.Data.Linq.ConflictMode.FailOnFirstConflict);
            context.Dispose();
            return newMap;
        }
    }

EDIT:

Here is the entity data model code :

using (MazeEntities ent = new MazeEntities())
        {
            ent.Connection.Open();
            return (from map in ent.Map
                    select map).ToList<Map>();
        }

This code runs properly if I did not use the web service before. If I use the web service first, it shows an error at ent.Connection.Open();

Here is the inner exception:

Cannot open user default database. Login failed.\r\nLogin failed for user 'erkape-PC\erkape'.

Connection string for web service :

connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\3DMapDatabase.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"

Connection string for the game:

"metadata=res:///MazeDataModel.csdl|res:///MazeDataModel.ssdl|res://*/MazeDataModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=D:\eRKaPe\DropBox\TA\Program\3D_Map_Editor\3DMapEditorServices\App_Data\3DMapDatabase.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />

4

4 回答 4

0

请查看以下帖子 http://th2tran.blogspot.ae/2009/06/underlying-provider-failed-on-open.html

也请在该应用程序的应用程序池中启用 32 位应用程序。

这可能会解决。

于 2014-03-05T12:07:41.067 回答
0

为了快速检查,您可以尝试在使用后添加以下行:

    using (BaseModelDataContext context = new BaseModelDataContext())
    {
        context.Connection.Open();

        OR

        context.Database.Connection.Open(); 

        // your code here
于 2012-05-12T07:45:20.720 回答
0

在阅读了一些文章后,我终于找到了解决问题的方法。关闭地图编辑器后,来自 Web 服务的连接不会自动关闭。这就是为什么我无法从游戏中访问我的数据库。我必须更改两个应用程序的连接字符串,我将用户实例设置为 False。游戏可以通过这种方式访问​​数据库。

于 2012-05-19T11:22:35.623 回答
-1

您正在尝试返回与上下文关联的对象(地图)。该对象具有无法返回给客户端的上下文信息。

您将需要创建自己的 DataContract(具有必要属性的类型),并希望将其公开给客户端。

或者您可以使用 POCO 实现 如此处所述

于 2012-05-12T06:39:12.463 回答