如何在实体框架

时间:2017-02-16 11:59:55

标签: asp.net-mvc entity-framework code-first

我正在尝试使用MVC中的代码优先方法制作示例应用程序,因此我没有任何数据库。现在我已经设置了所有类文件和上下文类。在下一步中,我创建了一个控制器(Emplty),并希望创建自己的创建/列表/删除功能。我怎么能这样做。

部分代码在

之下
        public class Employee
            {
                public int EmployeeID { get; set; }
                public string Name { get; set; }
                public int Age { get; set; }

                public int DepartmentID { get; set; }//Foreign Key

                public virtual ICollection<Enrollment> Enrollments { get; set; }
            }

      public class Department
        {
            public int DepartmentID { get; set; }
            public string DepartmentName { get; set; }

            public virtual ICollection<Enrollment> Enrollments { get; set; }
        }

     public class EmployeeDBContext: DbContext
        {
            public EmployeeDBContext()
                : base("EmployeeDBContext")//EmployeeDBContext will be name of database. 
            { }

            public virtual DbSet<Employee> Employees { get; set; }
            public virtual DbSet<Department> Departments { get; set; }
            public virtual DbSet<Enrollment> Enrollments { get; set; }
        }


 public class EmployeeInitializer:DropCreateDatabaseIfModelChanges<EmployeeDBContext>
    {
        protected override void Seed(EmployeeDBContext context)
        {
            var employee = new List<Employee>
            {
                new Employee{EmployeeID=1, Name="Aman", Age=15, DepartmentID=11},
                new Employee {EmployeeID=2, Name="Supriya", Age=12, DepartmentID=22},
                new Employee {EmployeeID=3, Name="Rishabh", Age=10, DepartmentID=44}
            };

            employee.ForEach(x => context.Employees.Add(x));
            context.SaveChanges();

            var department = new List<Department> {
            new Department{DepartmentID=11, DepartmentName="IT"},
            new Department{DepartmentID=22, DepartmentName="HR"},
            new Department{DepartmentID=33, DepartmentName="Mechanical"},
            new Department{DepartmentID=44, DepartmentName="NGO"}
            };

            department.ForEach(x=>context.Departments.Add(x));
            context.SaveChanges();

            var enrollment = new List<Enrollment>() { 
            new Enrollment{EnrollmentID=111, EmployeeID=1, DepartmentID=11},
            new Enrollment{EnrollmentID=222, EmployeeID=3, DepartmentID=44},
            new Enrollment{EnrollmentID=333, EmployeeID=2, DepartmentID=22}
            };
            enrollment.ForEach(x=>context.Enrollments.Add(x));
            context.SaveChanges();

        }   
    }

我希望从控制器下方添加代码

public ActionResult CreateEmployee(Employee employee)
{

   //my desired code here
    return View();
}


[HttpGet]
public ActionResult ListEmployee()
{
 //my desired code here
    return View();
}

1 个答案:

答案 0 :(得分:0)

你的意思是如何做:

   [HttpGet]
    public ActionResult ListEmployee()
    {
     //my desired code here
    using(var context=new EmployeeDBContext())
    {
     return View(context. Employee.ToList());
    }

    }

[HttpPost]
[Route("api/Employee/CreateEmployee")]
public ActionResult CreateEmployee([FromBody]Employee employee)
{
//add to ef
_db.Add(employee);
_db.SaveChanges();

   //my desired code here
    return Ok();
}