MVC 8
MVC 8
the view. The view then renders the data. Model can be entities or business
objects.
In this video, we will discuss, retrieving data from a database table tblEmployee
using entity framework. In a later video, we will discuss using business objects as
our model.
Step 1: Install entity framework, if you don't have it installed already on your
computer. At the time of this recording the latest version is 5.0.0.0. Using nuget
package manager, is the easiest way to install. A reference to EntityFramework.dll
is automatically added.
Open visual studio > Tools > Library Package Manager > Manage NuGet Packages for
Solution
Step 2: Add EmployeeContext.cs class file to the Models folder. Add the following
"using" declaration.
using System.Data.Entity;
Step 3: Add a connection string, to the web.config file, in the root directory.
<connectionStrings>
<add name="EmployeeContext"
connectionString="server=.; database=Sample; integrated security=SSPI"
providerName="System.Data.SqlClient"/>
</connectionStrings>
Step 4: Map "Employee" model class to the database table, tblEmployee using "Table"
attribute as shown below.
[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; }
}
return View(employee);
}
Step 6: Finally, copy and paste the following code in Application_Start() function,
in Global.asax file. Database class is present "in System.Data.Entity" namespace.
Existing databases do not need, database initializer so it can be turned off.
Database.SetInitializer<MVCDemo.Models.EmployeeContext>(null);
That's it, run the application and navigate to the following URL's and notice that
the relevant employee details are displayed as expected
http://localhost/MVCDemo/Employee/details/1
http://localhost/MVCDemo/Employee/details/2
Email This
BlogThis!
Share to Twitter
Share to Facebook
Share to Pinterest
94 comments:
[Table("tblEmployee")]
public class Employee
{
[Key]
public int EmpId { get; set; }
public byte EmpDeptId { get; set; }
public string EmpName { get; set; }
public DateTime EmpJoinDate { get; set; }
public char EmpGender { get; set; }
public string EmpCity { get; set; }
}
And Kept the EmpGender field in the Database Table as {nchar(1)}.
In this way I could not show the EmpGender in the view. But when I changed it from
char to string it worked fine.
How can I solve it if I want to put Gender as character as I like to insert only
'M' or 'F'?
Thanks
Reply
Replies
Reply
Reply
Hello Sir
[Table("Employee")]
public class Employee
{
[Key]
public int EmpId { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
}
2- First time when i run it then i get a log-in fail connection pooling error
so
- move in iis Manager.
- Open Application pool at left side of screen click it.
- then there is a pooling connection is defaultAppPool, select it.
- Click Advance setting link at right side menu bar.
- Change Identity property from ApplicationPoolIdentity to LocalSystem.
now run your application.
Reply
What are the changes we do for the above same with MySQL
Reply
[Table("tblEmployee")]
public class Employee
{
[Key]
public int EmployeeId { get; set; }
Reply
Reply
how to fix it
Reply
Source Error:
Line 17:
Line 18: EmployeeContext employeeContext = new EmployeeContext();
Line 19: Employee employee = employeeContext.Employees.Single(x => x.EmployeeId ==
id);
Line 20:
Line 21:
Reply
I received this error when I run program "An error occurred while getting provider
information from the database. This can be caused by Entity Framework using an
incorrect connection string. Check the inner exceptions for details and ensure that
the connection string is correct."
Reply
Reply
Reply
Steps:Go to Tools -> Library Package Manager->Package Manager Console and Type PM>
Install-Package EntityFramework -Version 5.0.0
Reply
Replies
Reply
Reply
Reply
Reply
Reply
UnknownFebruary 17, 2014 at 1:59 AM
use this code it is working
[Table("tblemp")]
Reply
Hi! I'm have an error: "An error occurred while getting provider information from
the database. This can be caused by Entity Framework using an incorrect connection
string. Check the inner exceptions for details and ensure that the connection
string is correct."
Can someone help me? Maybe someone have the same problem and know how to solve? I'm
will be very grateful.
Reply
Reply
Reply
Replies
Reply
please help me
Reply
Reply
"The parameters dictionary contains a null entry for parameter 'EmployeeId' of non-
nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult
Details(Int32)' in 'Mvcsecond.Controllers.EmployeeController'. An optional
parameter must be a reference type, a nullable type, or be declared as an optional
parameter.
Parameter name: parameters "
though i entered url: http://localhost:49979/Employee/Details/1
how can i resolve this error
Reply
Replies
Reply
Reply
Reply
AnonymousJuly 7, 2014 at 6:02 AM
Invalid object name 'dbo.Employees'. when In my db table name is Employee
Reply
Reply
Uninstall-package EntityFramework
Install-Package EntityFramework version 5.0.0
Happy Coding :)
Reply
Reply
Replies
It will work
Reply
Reply
Nisar AhmadSeptember 13, 2014 at 3:42 AM
I am Using Employees Table of Northwind Database and only using the fields given
below not all :
[Table("Employees")]
public class Employee
{
Reply
I had copied the code exactly and followed the video step by step too.
If some people have a working version, can they share it with us?
GO
Reply
Replies
User DeepCoding placed a message at this website and I found the link from
Microsoft website.
http://support2.microsoft.com/kb/2816241
Reply
Reply
Reply
Replies
Reply
Reply
When i ran this program am getting this error. Please let me know what is the
mistake.
[Table("employee")]
public class Employee
{
public int empid { get; set; }
public string Name { get; set; }
public string Location { get; set; }
}
Reply
Replies
Reply
the sample from the video didn't work for me as it installed EF 6 from MS.
I did it again using MVC 5 with VS2014 and Entity Framework 6
seems to be working.
Reply
Source Error:
What is Wrong????
Reply
Reply
Replies
Reply
Reply
Reply
" Sir , when i am working with multiple tables, it gives me the above mentioned
error.... sir, please find a solution for this.. thanks"
Reply
Reply
Reply
in the line
Employee employee = employeeContext.Employees.Single(emp => emp.EmployeeId == id);
can someone please help me to understand what is really going went through most of
the comments and found many having the trouble in the same line
Reply
in the line
Employee employee = employeeContext.Employees.Single(emp => emp.EmployeeId == id);
can someone please help me to understand what is really going went through most of
the comments and found many having the trouble in the same line
Reply
the code above is being highlighted and giving the error msg as below can someone
please help me so that i can get a move on
Reply
Replies
Reply
Reply
Please help me i am trying to learn all the tutorials but suck on this point.
Reply
Reply
SantanuMay 26, 2016 at 4:25 AM
Invalid value for key 'attachdbfilename'. Error Comes. What should i do ?
Reply
Reply
The parameters dictionary contains a null entry for parameter 'id' of non-nullable
type 'System.Int32' for method 'System.Web.Mvc.ActionResult Details(Int32)' in
'Views.Controllers.EmployeeController'. An optional parameter must be a reference
type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
Reply
Reply
Reply
Reply
Reply
Reply
Reply
return View(employee);
//return View(employee);
}
Dear Venkat,
I'm Alagu Ganesh and I'm using your code but the input is not int its is string you
can see that in this email.
I'm compile this and I'm getting this error.
your blog and your you-tube videos are very help full and I'm learning lot of new
software language.
Reply
Reply
Reply
Reply
Reply
Replies
Reply
The model item passed into the dictionary is of type 'mvcdemo.Models.Employee', but
this dictionary requires a model item of type
'System.Collections.Generic.IEnumerable`1[mvcdemo.Models.Employee]'.
Reply
And Here is the error i'm facing (not the complete error because not let me write
more than 4096 characters):
Reply
The parameters dictionary contains a null entry for parameter 'id' of non-nullable
type 'System.Int32' for method 'System.Web.Mvc.ActionResult Details(Int32)' in
'MVCDemo1.Controllers.EmployeeController'. An optional parameter must be a
reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
in Employee controller for this line of code I get the above error
I am using visual studio 2012 and SSMS 2012 (My connection string was not created
hence i added it explicitly as provided in part-8)
Reply
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
namespace WebApplication5.Models
{
[Table("Table")]
public class Employees
{
[key]
public int Id { get; set; }
public string Name { get; set; }
public string Gender { get; set;}
public string City { get; set; }
}
}
-------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication5.Models;
namespace WebApplication5.Controllers
{
public class EmployeeController : Controller
{
EmployeeDBEntities objEmployeeDBEntities = new EmployeeDBEntities();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace WebApplication5.Models
{
public class EmployeeContext :DbContext
{
public DbSet Employees { get; set; }
}
}
----------------------
Global.asax.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Data.Entity;
namespace WebApplication5
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
Database.SetInitializer(null);
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
}
-------------------------------------------------------------
Reply
Reply
Reply