实体框架中的自动投影

发布于 2025-01-08 19:50:55 字数 1122 浏览 3 评论 0原文

无论如何,有没有在实体框架中创建自动投影?请参阅:

public class Person{ 
    public int Id {get; set;} 
    public string FirstName {get; set;} 
    public string LastName {get; set;} 
    public string FatherName {get; set;} 
    public string City {get; set;} 
    public string AddressLine {get; set;} 
    public string Something {get; set;} 
}

public class PersonNameModel{
    public string FirstName {get; set;} 
    public string LastName {get; set;} 
    public string FatherName {get; set;} 
}

public class PersonAddressModel{
    public string City {get; set;} 
    public string AddressLine {get; set;} 
}

// etc...

我的意思是我能够替换普通投影,例如:

context.Persons.Select(t => new PersonNameModel{ FirstName = t.FirstName /* etc */ });

使用可以使用反射并创建自动投影的扩展方法,例如:

public static class MyExtensions{
    public static IQueryable<T> AutoSelect<T, TProject>(this IQueryable<T> q){
        // read TProject type in reflection
        // create a projection as a IQueryable<T> 
    }
}

有什么办法吗?我用谷歌搜索,但没有找到任何资源。你能指导我一下吗?

Is there anyway to create a auto projection in entity framework? see please:

public class Person{ 
    public int Id {get; set;} 
    public string FirstName {get; set;} 
    public string LastName {get; set;} 
    public string FatherName {get; set;} 
    public string City {get; set;} 
    public string AddressLine {get; set;} 
    public string Something {get; set;} 
}

public class PersonNameModel{
    public string FirstName {get; set;} 
    public string LastName {get; set;} 
    public string FatherName {get; set;} 
}

public class PersonAddressModel{
    public string City {get; set;} 
    public string AddressLine {get; set;} 
}

// etc...

I mean I be able to replace normal projection like:

context.Persons.Select(t => new PersonNameModel{ FirstName = t.FirstName /* etc */ });

whit an extension method that can use reflection and create an auto projection like:

public static class MyExtensions{
    public static IQueryable<T> AutoSelect<T, TProject>(this IQueryable<T> q){
        // read TProject type in reflection
        // create a projection as a IQueryable<T> 
    }
}

Is there any way? I googled it, but didn't find any resource. Can you guide me please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

写下不归期 2025-01-15 19:50:55

是的,可以将实体框架实体自动投影到某些 Dto。请参阅此处的实现之一 https://gist.github.com/1367880

其用作:

context.Persons.Project().To<PersonNameModel>().ToList();

您可以将 在这种情况下,将生成数据库查询以仅选择所需的列(由 PersonNameModel 指定)。

如果您只想映射查询结果(检索对象),那么 EmitMapper 或 AutoMapper 应该是您的选择。

Yes, it's possible to auto project entity framework entities to some Dto. See one of implementations here https://gist.github.com/1367880

You can use it as:

context.Persons.Project().To<PersonNameModel>().ToList();

In that case the db query will be generated to select only required columns (specified by PersonNameModel).

If you want just to map query results (that retrieved objects), so EmitMapper or AutoMapper should be your choice.

旧时光的容颜 2025-01-15 19:50:55

如果我正确理解你想要的是对象之间的映射,请使用 Automapper 它会为你进行映射

http ://www.codeproject.com/Articles/61629/AutoMapper

http://automapper.org/

git hub 路径 https://github.com/AutoMapper/AutoMapper

If I understand correctly what you want is a mapping between objects, use Automapper it would do the mapping for you

http://www.codeproject.com/Articles/61629/AutoMapper

http://automapper.org/

git hub path https://github.com/AutoMapper/AutoMapper

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文