Entify Frame Work不先使用代码保存数据-ASP.NET WebForm

时间:2019-03-18 11:56:11

标签: c# asp.net entity-framework webforms entity-framework-6

我是EF的新手,需要使用asp.net Webform 4.5中的EF进行更改

我能够在转发器控件中显示数据,在下拉列表中提取数据,但是由于某种原因,我无法为Employee保存数据,我没有收到任何错误,并且当我单击SQL Profiler时,它不显示任何Insert存储过程在显示的详细信息列表中。

我做错了什么还是我的方法有误

 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);

    }
}

1 个答案:

答案 0 :(得分:2)

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; }