3
Answers

Asp.net MVC: the underlying provider failed to open.

Photo of sangeetha k

sangeetha k

7y
170
1
Help me where I am wrong
 
Here with I attach my mvc code.
 
#view
  1. @model Mvcpractice1.Models.Employee  
  2. @{  
  3. ViewBag.Title = "Employee Details";  
  4. }  
  5. <h2>Employee Details</h2>  
  6. <table>  
  7. <tr>  
  8. <td>  
  9. <b>Employee Id:</b>  
  10. </td>  
  11. <td>  
  12. @Model.EmployeeId  
  13. </td>  
  14. </tr>  
  15. <tr>  
  16. <td><b>Employee Phoneno:</b></td>  
  17. <td>@Model.Phoneno</td>  
  18. </tr>  
  19. <tr>  
  20. <td><b>Employee Name:</b></td>  
  21. <td>@Model.Name</td>  
  22. </tr>  
  23. </table>  
#controller
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using Mvcpractice1.Models;  
  7. namespace Mvcpractice1.Controllers  
  8. {  
  9. public class EmployeeController : Controller  
  10. {  
  11. //  
  12. // GET: /Employee/  
  13. public ActionResult Index( int id)  
  14. {  
  15. EmployeeContext emp=new EmployeeContext();  
  16. Employee emp1= emp.Employees.Single(x=>x.EmployeeId == id);  
  17. return View(emp1);  
  18. }  
  19. }  
  20. }  
#DbContetxt
  1. namespace Mvcpractice1.Models  
  2. {  
  3. public class EmployeeContext:DbContext  
  4. {  
  5. public DbSet<Employee> Employees { getset; }  
  6. }  
  7. }  
#webConfig
  1. <connectionStrings>  
  2. <add name="EmployeeContext" connectionString="Data Source=CHV8LTDBNC01\LENTRACSDB2008;Integrated Security=True ;Database=AdventureWorks2008" providerName="System.Data.SqlClient" />  
  3. </connectionStrings>  
But this is yeilding
 
An exception of type 'System.Data.Entity.Core.EntityException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
Additional information: The underlying provider failed on Open.

Answers (3)

0
Photo of mari muthu
NA 160 20.6k 7y
change this from your code(#DbContetxt);
  1. namespace Mvcpractice1.Models  
  2. {  
  3. public class EmployeeContext:DbContext  
  4. {  
  5. public EmployeeContext:base("EmployeeContext")  
  6. {  
  7.   
  8. }  
  9. public DbSet<Employee> Employees { getset; }  
  10. }  
  11. }  
0
Photo of Ramesh Palanivel
NA 9.5k 138.7k 7y
Hi Sangeetha,
 
Kindly check whether your database credentials are correct? The issue will happen due to inproper connection. 
0
Photo of mari muthu
NA 160 20.6k 7y
hi,
 
Seems like a connection issue. You can use the Data link properties to find if the connection is fine. Do the following:
  • Create a blank notepad and rename it to "X.UDL"
  • Double click to open it
  • Under connections tab choose the server name/enter the name use the correct credentials and DB
  • Click OK to save it.
Now open the file in Notepad and compare the connection string properties.