gh9*_*gh9 3 c# entity-framework ef-code-first
我试图通过Code First和EF 5.0加载导航属性子对象加载为null.下面是代码.
[Table("ls_roles")]
public class Role
{
[Required]
[Key]
public int RoleID { get; set; }
[Required]
public String BarColor { get; set; }
[ForeignKey("RoleId")]
public virtual ICollection<ScheduleEmployee> Employees { get; set; }
}
[Table("ls_ScheduleEmployee")]
public class ScheduleEmployee
{
[Key]
[Required]
public int Id { get; set; }
[Required]
public int RoleId { get; set; }
[ForeignKey("RoleId")]
public Role Role { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
编辑:调用代码
class Program
{
static void Main(string[] args)
{
var x = new Model.ContextEntityFramework().ScheduleEmployees.FirstOrDefault();
}
}
Run Code Online (Sandbox Code Playgroud)
x.Role ==此时为null
您必须对您的主叫代码执行.include以包含子项.
就像是
Model.ContextEntityFramework().ScheduleEmployees.Include(x => x.Role).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3759 次 |
| 最近记录: |