erkape
erkape

Reputation: 66

System Data Entity. The underlying provider failed on Open

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" />

Upvotes: 0

Views: 3897

Answers (4)

Sheryar Nizar
Sheryar Nizar

Reputation: 305

Please check the following post http://th2tran.blogspot.ae/2009/06/underlying-provider-failed-on-open.html

Also please Enable for 32 Bit application in the APplication Pool of that application.

This may resolve.

Upvotes: 0

erkape
erkape

Reputation: 66

Finally I found a way to solve my problem after reading some articles. The connection from the web service doesn't close automatically after I close the map editor. That is why I can't access my database from the game. I have to change the connection string from both application, I set the User Instance to False. The game can access the database this way.

Upvotes: 0

Hps
Hps

Reputation: 1177

For a quick check, can you try adding the following line after the using:

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

        OR

        context.Database.Connection.Open(); 

        // your code here

Upvotes: 0

Hps
Hps

Reputation: 1177

You are trying to return an object (Map) which is associated with the Context. This object has the context information which can't be returned to the client.

You will need to create your own DataContract (a type having necessary properties) that you want to expose to the client.

Or you can use the POCO implementation As described here

Upvotes: -1

Related Questions