Entity Framework .Include() with compile time checking?

后端 未结 5 1388
误落风尘
误落风尘 2020-12-15 11:11

Consider the following code, which is calling against an EF generated data context:

var context = new DataContext();
var employees = context.Employees.Includ         


        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-15 11:39

    In case you're still using versions prior to Entity Framework 5, the good news is as of C# 6, you can now use nameof to retrieve name of any class/object.

    So now you may do

    var context = new DataContext();
    var employees = context.Employees.Include(nameof(Employees.Department));
    

    If you're using EF > 5, then Xavier's answer is better

提交回复
热议问题