Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 错误:类型为';System.ArgumentException';发生在System.Data.dll中,但未在用户代码中处理_C#_Asp.net_.net_Asp.net Mvc_Linq - Fatal编程技术网

C# 错误:类型为';System.ArgumentException';发生在System.Data.dll中,但未在用户代码中处理

C# 错误:类型为';System.ArgumentException';发生在System.Data.dll中,但未在用户代码中处理,c#,asp.net,.net,asp.net-mvc,linq,C#,Asp.net,.net,Asp.net Mvc,Linq,我只是MVC的新手,通过在线教程学习。在我的代码中,我只是使用实体框架从数据库中检索数据。我在模型中添加了[Key]属性,但仍然面临异常错误。运行应用程序还需要做什么?真的,我需要帮助 控制器: public class EmployeeDetailsController : Controller { public ActionResult details(int id) { EmployeeContext employeecontext = new Employ

我只是MVC的新手,通过在线教程学习。在我的代码中,我只是使用实体框架从数据库中检索数据。我在模型中添加了
[Key]
属性,但仍然面临异常错误。运行应用程序还需要做什么?真的,我需要帮助

控制器:

public class EmployeeDetailsController : Controller
{
    public ActionResult details(int id)
    {
        EmployeeContext employeecontext = new EmployeeContext();
        Employee employee = employeecontext.Employees.Single(emp => emp.emp_id == id);
        return View(employee);
    }
}
Employeecontext.cs

public class EmployeeContext : DbContext
{
    public DbSet<Employee> Employees { get; set; }
}
查看(details.cs.html):

。。。。
员工详细信息
客户id:@Model.emp\u id
客户名称:@Model.emp\u name
客户电子邮件:@Model.emp_电子邮件
Web.config:

<connectionStrings>
    <add name="EmployeeContext"
        connectionString="Data Source=192.168.4.201;Initial Catalog=nhphealthnew1;User ID=teamaardee;Password=team@aardee#1234#!" 
        providerName="System.Data.SqlClient" />
  </connectionStrings>

像这样试试看

try{
    EmployeeContext employeecontext = new EmployeeContext ();
    Employee employee = employeecontext.Employees.Where( e=> e.ID == id ).First();
}
catch (Exception ex)
{
    Console.Writeline ("Exception: " + ex.toString());
}
请把输出贴在这里。
哦,别忘了检查您的“nhphealthnew1”数据库中是否有一个名为“tbl_employee”的表,该表的列是否正确

iam遵循相同的教程,ia也遇到了这个问题,我通过在web.config中添加此cod找到了解决方案

  <connectionStrings>
  <add name="EmployeeContext"  
        providerName="System.Data.SqlClient"  
        connectionString="Server=.\SQLEXPRESS;Database=Sample;Integrated Security=True;"/>
</connectionStrings>


调试您的代码!哪行代码引发异常,以及异常的详细信息是什么。
Employee.cs:
是EF的自动生成类?调试后,代码在Employeedetailscontroller中出错,“Employee-Employee=employeecontext.Employees.Single(emp=>emp.emp\u id==id);”错误:EntityFramework.SqlServer.dll中发生“System.Data.Entity.Core.EntityCommandExecutionException”类型的异常,但未在用户代码中处理其他信息:执行命令定义时出错。有关详细信息,请参见内部异常。@M Adeel Khalid,先生,我创建了一个类,在其中定义了字段。请提供内部异常详细信息。注意EF列名和属性约束,它们需要与数据库属性同步。@Elvin不管怎么样,我都要试试这个。。!!!
try{
    EmployeeContext employeecontext = new EmployeeContext ();
    Employee employee = employeecontext.Employees.Where( e=> e.ID == id ).First();
}
catch (Exception ex)
{
    Console.Writeline ("Exception: " + ex.toString());
}
  <connectionStrings>
  <add name="EmployeeContext"  
        providerName="System.Data.SqlClient"  
        connectionString="Server=.\SQLEXPRESS;Database=Sample;Integrated Security=True;"/>
</connectionStrings>