简体   繁体   中英

Connecting ADO.Net entity data model to database

I have a WPF application using an ADO.NET entity data model. After creating the edmx , a connection string is by default created in app.config like this:

<add name="Entities" 
     connectionString="metadata=res://*/Model.DataAccess.DataModel.csdl|res://*/Model.DataAccess.DataModel.ssdl|res://*/Model.DataAccess.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=servername;initial catalog=databasename;integrated security=True;user id=username;multipleactiveresultsets=True;application name=EntityFramework&quot;" 
     providerName="System.Data.EntityClient" />

I use the entities in the code behind as follows:

Entities ent = new Entities();
var tes = (from e in ent.testtable
select e.name).tolist();

After I released the application, the users are not able to connect to the database. They get the error

The underlying provider failed on Open.

The users does not have access to the database, however since the credentials in the config file has the access, shouldn't the users be able to access the database.

Please let me know if I am missing something here, as I am not able to figure out what I am doing wrong.

You have both the Integrated Security=True as well as User id=username; in your "provider connection string".

Don't do this!

If you do, the Integrated Security=true will have precedence, so right now, the connection is attempted with the currently logged in user credentials.

You need to remove the Integrated Security=true from your provider connection string!

provider connection string=&quot;data source=servername;initial catalog=databasename;integrated security=True;user id=username;multipleactiveresultsets=True;application name=EntityFramework&quot;
                                                                                     ************************  <--- remove this part  

Use this connection string instead:

<add name="Entities" 
     connectionString="metadata=res://*/Model.DataAccess.DataModel.csdl|res://*/Model.DataAccess.DataModel.ssdl|res://*/Model.DataAccess.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=servername;initial catalog=databasename;user id=username;multipleactiveresultsets=True;application name=EntityFramework&quot;" 
     providerName="System.Data.EntityClient" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM