实体框架不先使用代码保存数据 - asp.net webform
Entify Frame work not saving data using code first - asp.net webform
我是 EF 的新手,需要在 asp.net webform 4.5
中更改现有的 EF
我能够在转发器控件中显示数据,在下拉列表中拉取数据,但由于某种原因我无法为员工保存数据,我没有收到任何错误,当我崩溃时 SQL Profiler 就这样了它显示的详细信息列表中没有显示任何插入存储过程。
是我做错了什么还是我的方法不对
App_Code
DBClass
Department.cs
Employee.cs
EmployeeDBContent.cs
EmpRepository.cs
所有文件的代码
EmployeeDBContext.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using empNS;
/// <summary>
/// Summary description for EmployeeDBContext
/// </summary>
///
namespace empNS
{
public class EmployeeDBContext : DbContext
{
public DbSet<Department> Departments { get; set; }
public DbSet<Employee> Employees { get; set; }
public EmployeeDBContext()
: base("EmployeeDBContext")
{
//disable initializer
Database.SetInitializer<EmployeeDBContext>(null);
}
}
//protected override void OnModelCreating(DbModelBuilder modelBuilder)
//{
// base.OnModelCreating(modelBuilder);
//}
}
EmpRepository.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for EmpRepository
/// </summary>
///
namespace empNS
{
public class EmpRepository
{
public static List<Employee> GetEmployees()
{
EmployeeDBContext empDBContext = new EmployeeDBContext();
return empDBContext.Employees.ToList();
}
public static List<Department> GetDepartments()
{
EmployeeDBContext empDBContext = new EmployeeDBContext();
return empDBContext.Departments.ToList();
}
public static List<Department> GetDepartmentNames()
{
EmployeeDBContext empDBContext = new EmployeeDBContext();
return empDBContext.Departments.ToList();
}
public static void InsertEmployee(Employee employee )
{
EmployeeDBContext empDBContext = new EmployeeDBContext();
empDBContext.SaveChanges();
}
}
}
Department.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Dept
/// </summary>
///
namespace empNS
{
public class Department
{
//Scalar properties
public int Id { get; set; }
public string Name { get; set; }
public string Location { get; set; }
//Navigation Property
public List<Employee> Employees { get; set; }
}
}
employee.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
/// <summary>
/// Summary description for Emp
/// </summary>
///
namespace empNS
{
public class Employee
{
//Scalar Properties
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Designation { get; set; }
public int Department_Id { get; set; }
//Navigation Property
[ForeignKey("Department_Id")]
public Department Department { get; set; }
}
}
Employee.aspx
<h1>Employee List</h1>
<asp:Repeater ID="rptEmpList" runat="server" >
<ItemTemplate>
<p><%#Eval("Id") %> | <%#Eval("FirstName") %> | <%#Eval("LastName") %></p>
</ItemTemplate>
</asp:Repeater>
<h1>SAVE EMPLOYEE</h1>
<p>FN: <asp:TextBox ID="txtFN" runat="server"></asp:TextBox></p>
<p>LN: <asp:TextBox ID="txtLN" runat="server"></asp:TextBox></p>
<p>Des: <asp:TextBox ID="txtDes" runat="server"></asp:TextBox></p>
<p>Dept: <asp:DropDownList ID="ddDept" runat="server"></asp:DropDownList></p>
<p><asp:Button ID="btnSaveEmployee" runat="server" Text="Save Employee" OnClick="btnSaveEmployee_Click" /></p>
Employee.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using empNS;
public partial class EmployeePage : System.Web.UI.Page
{
Employee empObj = new Employee();
protected void Page_Load(object sender, EventArgs e)
{
rptEmpList.DataSource = EmpRepository.GetEmployees();
rptEmpList.DataBind();
//fill DD
getDepartmentNames();
}
public void getDepartmentNames()
{
ddDept.DataSource = EmpRepository.GetDepartmentNames();
ddDept.DataTextField = "Name";
ddDept.DataValueField = "Id";
ddDept.DataBind();
}
protected void btnSaveEmployee_Click(object sender, EventArgs e)
{
empObj.FirstName = txtFN.Text;
empObj.LastName = txtLN.Text;
empObj.Designation = txtDes.Text;
empObj.Department_Id = int.Parse(ddDept.SelectedItem.Value.ToString());
EmpRepository.InsertEmployee(empObj);
}
}
这个
public static void InsertEmployee(Employee employee )
{
EmployeeDBContext empDBContext = new EmployeeDBContext();
empDBContext.SaveChanges();
}
需要这样:
public static void InsertEmployee(Employee employee )
{
EmployeeDBContext empDBContext = new EmployeeDBContext();
empDBContext.Employees.Add(employee);
empDBContext.SaveChanges();
}
编辑
确保您也正确声明了 DbSet。
在你的情况下,它应该是这样的:
DbSet<Employee> Employees { get; set; }
我是 EF 的新手,需要在 asp.net webform 4.5
中更改现有的 EF我能够在转发器控件中显示数据,在下拉列表中拉取数据,但由于某种原因我无法为员工保存数据,我没有收到任何错误,当我崩溃时 SQL Profiler 就这样了它显示的详细信息列表中没有显示任何插入存储过程。
是我做错了什么还是我的方法不对
App_Code
DBClass
Department.cs
Employee.cs
EmployeeDBContent.cs
EmpRepository.cs
所有文件的代码
EmployeeDBContext.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using empNS;
/// <summary>
/// Summary description for EmployeeDBContext
/// </summary>
///
namespace empNS
{
public class EmployeeDBContext : DbContext
{
public DbSet<Department> Departments { get; set; }
public DbSet<Employee> Employees { get; set; }
public EmployeeDBContext()
: base("EmployeeDBContext")
{
//disable initializer
Database.SetInitializer<EmployeeDBContext>(null);
}
}
//protected override void OnModelCreating(DbModelBuilder modelBuilder)
//{
// base.OnModelCreating(modelBuilder);
//}
}
EmpRepository.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for EmpRepository
/// </summary>
///
namespace empNS
{
public class EmpRepository
{
public static List<Employee> GetEmployees()
{
EmployeeDBContext empDBContext = new EmployeeDBContext();
return empDBContext.Employees.ToList();
}
public static List<Department> GetDepartments()
{
EmployeeDBContext empDBContext = new EmployeeDBContext();
return empDBContext.Departments.ToList();
}
public static List<Department> GetDepartmentNames()
{
EmployeeDBContext empDBContext = new EmployeeDBContext();
return empDBContext.Departments.ToList();
}
public static void InsertEmployee(Employee employee )
{
EmployeeDBContext empDBContext = new EmployeeDBContext();
empDBContext.SaveChanges();
}
}
}
Department.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Dept
/// </summary>
///
namespace empNS
{
public class Department
{
//Scalar properties
public int Id { get; set; }
public string Name { get; set; }
public string Location { get; set; }
//Navigation Property
public List<Employee> Employees { get; set; }
}
}
employee.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
/// <summary>
/// Summary description for Emp
/// </summary>
///
namespace empNS
{
public class Employee
{
//Scalar Properties
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Designation { get; set; }
public int Department_Id { get; set; }
//Navigation Property
[ForeignKey("Department_Id")]
public Department Department { get; set; }
}
}
Employee.aspx
<h1>Employee List</h1>
<asp:Repeater ID="rptEmpList" runat="server" >
<ItemTemplate>
<p><%#Eval("Id") %> | <%#Eval("FirstName") %> | <%#Eval("LastName") %></p>
</ItemTemplate>
</asp:Repeater>
<h1>SAVE EMPLOYEE</h1>
<p>FN: <asp:TextBox ID="txtFN" runat="server"></asp:TextBox></p>
<p>LN: <asp:TextBox ID="txtLN" runat="server"></asp:TextBox></p>
<p>Des: <asp:TextBox ID="txtDes" runat="server"></asp:TextBox></p>
<p>Dept: <asp:DropDownList ID="ddDept" runat="server"></asp:DropDownList></p>
<p><asp:Button ID="btnSaveEmployee" runat="server" Text="Save Employee" OnClick="btnSaveEmployee_Click" /></p>
Employee.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using empNS;
public partial class EmployeePage : System.Web.UI.Page
{
Employee empObj = new Employee();
protected void Page_Load(object sender, EventArgs e)
{
rptEmpList.DataSource = EmpRepository.GetEmployees();
rptEmpList.DataBind();
//fill DD
getDepartmentNames();
}
public void getDepartmentNames()
{
ddDept.DataSource = EmpRepository.GetDepartmentNames();
ddDept.DataTextField = "Name";
ddDept.DataValueField = "Id";
ddDept.DataBind();
}
protected void btnSaveEmployee_Click(object sender, EventArgs e)
{
empObj.FirstName = txtFN.Text;
empObj.LastName = txtLN.Text;
empObj.Designation = txtDes.Text;
empObj.Department_Id = int.Parse(ddDept.SelectedItem.Value.ToString());
EmpRepository.InsertEmployee(empObj);
}
}
这个
public static void InsertEmployee(Employee employee )
{
EmployeeDBContext empDBContext = new EmployeeDBContext();
empDBContext.SaveChanges();
}
需要这样:
public static void InsertEmployee(Employee employee )
{
EmployeeDBContext empDBContext = new EmployeeDBContext();
empDBContext.Employees.Add(employee);
empDBContext.SaveChanges();
}
编辑
确保您也正确声明了 DbSet。 在你的情况下,它应该是这样的:
DbSet<Employee> Employees { get; set; }