简体   繁体   English

扩展实体框架结果

[英]Extension for Entity Framework result

I am working with Entity Framework and was wondering if there is a generic way to create extensions for query result. 我正在使用Entity Framework,并且想知道是否存在创建查询结果扩展的通用方法。

For example I have a method : 例如我有一个方法:

    public static Student GetStudent(selector)
    {
         return _context.Student.Where(selector).FirstOrDefault();
    }
...
    public void DoSomeWork()
    {
     var student=GetStudent(x=>x.id=123);
     student.name="Tom";
     student.Save();  // <---- is there a way to create such an extension. 
                      //Not for Student Entity only but for all entities.
    }

Thanks. 谢谢。

   public static Student GetStudent(Func<Student,bool> selector)
    {
         return _context.Student.Where(selector).FirstOrDefault();
    }
...
    public void DoSomeWork()
    {
     var student=GetStudent(x=>x.id==123);
     student.name="Tom";
     student.Save();  // <---- is there a way to create such an extension. 
                      //Not for Student Entity only but for all entities.
    }

Your EF repository should have a .tt file that is used to generate the EF Result classes. 您的EF储存库应具有一个.tt文件,该文件用于生成EF结果类。 You can modify that file so that all of your EF Generated types implement an interface (of your choosing), you can then write your extension method for your chosen interface. 您可以修改该文件,以便所有EF生成的类型都实现一个(您选择的)接口,然后可以为您选择的接口编写扩展方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM