如何在不materializaing实体检索实体按键? [英] How to retrieve entity keys without materializaing entities?

查看:102
本文介绍了如何在不materializaing实体检索实体按键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的是这样的:

context.EntitiesDbSet.Keys.Where(predicate);

和$ P $这里pdicate的类型是防爆pression< Func键<实体,布尔>> 我知道,现在唯一的解决办法是使用通过metada分析生成的预测。 有没有简单的方法?

And predicate here is of type Expression<Func<Entity,bool>> The only solution I know for now is to use projections generated via metada analysis. Are there any simpler methods?

推荐答案

我知道一种方法是通过反射,利用KeyAttribute对实体和搜索实体KeyAttribute的财产。例如:

One way I know is through reflection, using KeyAttribute on Entities and search on Entity the property with KeyAttribute. Example:

using System;
using System.ComponentModel.DataAnnotations;

namespace HelloWorld
{
    public class MyEntity
    {
        [Key]
        public int EntityID { get; set; }
        public string Name { get; set; }
    }

    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Type myType = typeof(MyEntity);

            var myProps = myType.GetProperties().ToList()
                .Where(prop => prop.GetCustomAttributes(true).Any(a => a is KeyAttribute));

            foreach (var element in myProps) {
                Console.WriteLine(element.Name);
            }
        }
    }
}

我相信会有所帮助。

I believe that would help.

这篇关于如何在不materializaing实体检索实体按键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆