简体   繁体   English

基于类型的实体框架通用查找

[英]Entity Framework Generic Lookup based on Type

I have a database with a number of tables which are purely read only lookups.我有一个包含许多表的数据库,这些表是纯粹的只读查找。 They're all 2 columns: Id & Description of the same type.它们都是 2 列:相同类型的 ID 和描述。

I want to create a generic method that takes the DbSet Type as T and returns from the appropriate table into a standard LookupModel class.我想创建一个通用方法,它将 DbSet 类型作为 T 并从适当的表返回到标准 LookupModel 类中。

Here's what I've got:这是我所拥有的:

    public List<LookupModel> GetLookupList<T>() where T : DbSet
    {
        try
        {
            using (var context = new TwoCEntities())
            {
                var rows = context.Set<T>();
                return rows.Select(row => new LookupModel
                {
                    Id = (int)row.GetType().GetProperty("Id").GetValue(row, null),
                    Description = row.GetType().GetProperty("Description").GetValue(row, null).ToString()
                }).ToList();
            }
        }
        catch (Exception e)
        {
            if (log.IsErrorEnabled) log.ErrorFormat("GetLookupList<{0}> raised exception {1} with message {2}", typeof(T), e.GetType(), e.Message);
            throw;
        }
    }

My problem is calling it.我的问题是调用它。 This does not compile:这不编译:

var result =  lookupRepository.GetLookupList<DbSet<BedType>>();

I get this error我收到这个错误

The type 'System.Data.Entity.DbSet<Repository.BedType>' cannot be used as type parameter 'T' in the generic type or method 'ILookupRepository.GetLookupList<T>()'. There is no implicit reference conversion from 'System.Data.Entity.DbSet<Repository.BedType>' to 'System.Data.Entity.DbSet'.

BedType is defined in my context as BedType 在我的上下文中定义为

public virtual DbSet<BedType> BedType { get; set; }

Could someone suggest where I've gone wrong?有人可以建议我哪里出错了吗?

The code that you have provided is nesting the DbSet .您提供的代码是嵌套DbSet

What you have shown is resolving T to this:您所展示的是将T解析为:

context.Set<DbSet<BedType>>();

Your T needs to be a plain entity class.你的T需要是一个普通的实体类。 Remove the DbSet generic constraint on the GetLookup method and add the class one.删除GetLookup方法上的DbSet泛型约束并添加class一。

public List<LookupModel> GetLookupList<T>() where T : class
{
    try
    {
        using (var context = new TwoCEntities())
        {
            var rows = context.Set<T>();
            return rows.Select(row => new LookupModel
            {
                Id = (int)row.GetType().GetProperty("Id").GetValue(row, null),
                Description = row.GetType().GetProperty("Description").GetValue(row, null).ToString()
            }).ToList();
        }
    }
    catch (Exception e)
    {
        if (log.IsErrorEnabled) log.ErrorFormat("GetLookupList<{0}> raised exception {1} with message {2}", typeof(T), e.GetType(), e.Message);
        throw;
    }
}

This will have your type variable T resolve to the following within the method.这将使您的类型变量T在方法中解析为以下内容。

context.Set<BedType>();

When you call it use it like this:当你调用它时,像这样使用它:

var result = lookupRepository.GetLookupList<BedType>();

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

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