【问题标题】:Error: The type 'TEntity' must be a reference type错误:类型“TEntity”必须是引用类型
【发布时间】:2013-04-01 08:51:28
【问题描述】:
public partial class GridMastercontrol<TEntity> : UserControl
{
private System.Data.Objects.ObjectSet<TEntity> _osMain; // error here
}

我无法声明泛型类型的对象集。 请帮助我如何做到这一点。

错误信息是- 类型“TEntity”必须是引用类型才能将其用作泛型类型或方法“System.Data.Objects.ObjectSet”中的参数“TEntity”

【问题讨论】:

    标签: entity-framework generics reflection


    【解决方案1】:

    在你的类声明中添加通用约束:

    public partial class GridMastercontrol<TEntity> : UserControl where TEntity : class
    

    你必须这样做,因为ObjectSet&lt;TEntity&gt; 已经有一个:

    public class ObjectSet<TEntity> : ObjectQuery<TEntity>, 
            IObjectSet<TEntity>, IQueryable<TEntity>, IEnumerable<TEntity>, 
            IQueryable, IEnumerable
    where TEntity : class
    

    【讨论】:

    • 虽然,我昨天自己解决了我的问题。但是,感谢您的解决方案。它有效...
    猜你喜欢
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    • 2023-01-17
    • 1970-01-01
    相关资源
    最近更新 更多