我目前正在使用C#中的代码第一个实体框架,我正在寻找一个函数来获取实体名称(不是属性名称,但是类的名称),当我拥有的是外键时。像这样的东西可能有反射或类似的东西吗?
我有以下实体(简化)。
public class Employee
{
public int EmployeeID { get; set; }
public string Name { get; set; }
public int DepertmentID { get; set; }
public virtual Department Department { get; set; }
}
public class Department
{
public int DepartmentID { get; set; }
public string Name { get; set; }
}
在OnModelCreation中,关系映射如下。
HasRequired(t => t.Department)
.WithRequired(t => t.Employee)
.HasForeignKey(d => d.DepartmentID);
所以,如果我有DepartmentID,我想把类名作为字符串。 ( “部门”)。