1

In an ASP.NET MVC web application, I have created the following entity:

[Table("tblEmployee")]
public class Employee
{
    public int EmployeeId { get; set; }
    public string Name { get; set; }
    public string Gender { get; set; }
    public string City { get; set; }
}

But, when I try to retrieve data from a database table called tblEmployee using Entity Framework, I get an error. What I have done until now is:

  • Created a database MVCDemo with "." as server name and using Windows authentication containing a table called tblEmployee
  • Installed Entity Framework
  • Added EmployeeContext.cs class file to Models folder

Code:

namespace MVCDemo.Models
{
    public class EmployeeContext : DbContext
    {
        public DbSet<Employee> Employees { get; set; }
    }
} 
  • Added a connection string to web.config file in the root directory

     <connectionSrtings>
         <add name="EmployeeContext" 
              connectionString="server=.; database=MVCDemo; integrated security=SSPI"
              providerName="System.Data.SqlClient;" />
     </connectionSrtings>
    
  • Added Details actionResult to EmployeeController to show employee details:

      namespace MVCDemo.Controllers
      {
          public class EmployeeController : Controller
          {
              // GET: Employee
              public ActionResult Details(int id)
              {
                  EmployeeContext employeeContext = new EmployeeContext();
                  Employee employee = employeeContext.Employees.Single(e => e.EmployeeId == id);
                  return View(employee);
              }
          }
      }
    

Finally, I added the following code to Global.asax to prevent initialization:

Database.SetInitializer<MVCDemo.Models.EmployeeContext>(null);

The problem is when I run the application I get this error:

HTTP Error 500.19 - Internal Server Error

The requested page cannot be accessed because the related configuration data for the page is invalid.

and when I comment connection strings out and try to reach

http://localhost:60613/Employee/Details/1 

to show details of 1st employee, I get this error:

System.Data.Entity.Core.EntityException: 'The underlying provider failed on Open.'SqlException: Cannot attach the file 'C:\Users\arya\source\repos\MVCDemo\MVCDemo\App_Data\MVCDemo.Models.EmployeeContext.mdf' as database 'MVCDemo.Models.EmployeeContext'.

3 Answers 3

1

Check your tag name, it is incorrect. It will trigger the error definitely.

<connectionSrtings>

It should be:

<connectionStrings>

Update: Since you have another issue, fix the last part of your connection string:

providerName="System.Data.SqlClient;

Remove the semi-colon at the end of SqlClient.

Sign up to request clarification or add additional context in comments.

2 Comments

It is a shame but true. I cant tell how disappointed I am and by the way, it is odd that I didn't get error for that misspell. But after getting that fixed, now I am facing this error: The ADO.NET provider with invariant name 'System.Data.SqlClient;' is either not registered in the machine or application config file, or could not be loaded. See the inner exception for details.
Dear David, thanks a lot. It worked. I accepted your answer, hope you do so.
0

This is because if you have the database named MVCDemo in the SQL Server then its fine, otherwise code first approach looking for MVCDemo database in SQL Server. If you dont have the database in SQL Server, then try this connection string to create the mdf file first.

connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-MVCDemo-20200820010246.mdf;Initial Catalog=aspnet-MVCDemo-20200820010246;Integrated Security=True"

1 Comment

Yes I want to have database on sqlServer but it seems that database is not connected to project to be read and I am getting this error now: The ADO.NET provider with invariant name 'System.Data.SqlClient;' is either not registered in the machine or application config file, or could not be loaded. See the inner exception for details.
0

First of all, I couldn't see your database connection. Yes you have teached the table but you didn't tell the program where to put those infos. Something like:

optionsBuilder.UseSqlServer("Data Source=databaseName") 

Second possible reason is your program don't know where to go at the beginning. I had those error before and solved it with using default map.

Third reason, this probably not true but, you wrote "connectionSrtings" wrong. As I say, it's probably not that but I wanted to mention if it is.

3 Comments

Thanks for reply. Its a shame I misspelled connection string but after getting that fixed, this error is what I see: The ADO.NET provider with invariant name 'System.Data.SqlClient;' is either not registered in the machine or application config file, or could not be loaded. See the inner exception for details. any suggestions?
We can help you better if you share your database connection with us. You probably missed that. You must tell the system the database you want to use. If you won't do that, system can't know the path.
Yes, sure. While creating database, I chose . for Server Name and Windows authentication for Authentication, created database and did as explained. Thank you again and let me know if I can provide any other information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.