Я новичок в EF и мне нужно внести изменения в существующие с EF в asp.net webform 4.5
Я достиг точки, где я могу читать данные из таблицы с помощью EF, в то время как, узнав, я понял, что яМне нужно указать FK Department_ID
в файле класса emp.cs после внесения этих изменений. Я получаю сообщение об ошибке, поскольку модель изменилась, что вполне очевидно.
Я попытался выполнить следующий код в файле EmployeeDBContext.cs1
, но получил ошибкуболее void
в нижнем классе
Сообщение об ошибке компилятора:
CS1518: Expected class, delegate, enum, interface, or struct
//protected override void OnModelCreating(DbModelBuilder modelBuilder)
//{
// base.OnModelCreating(modelBuilder);
//}
Теперь мой вопрос основан на приведенном ниже коде.
- Как сохранить
Employee
подробности - Как внести изменения в класс модели с удалением базы данных
Я правильно это делаю?
App_Code
DBClass
Dept.cs
Emp.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();
}
}
}
Dept.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; }
}
}
Emp.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();
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
// empObj.f
empObj.FirstName = txtFN.Text;
empObj.LastName = txtLN.Text;
empObj.Designation = txtDes.Text;
// empObj.Department = int.Parse("1");
}
}