C# 如何使用IObjectSet初始化此代码?

C# 如何使用IObjectSet初始化此代码?,c#,entity-framework-4,C#,Entity Framework 4,我有以下代码: public interface IEntityBase<T> where T : struct, IComparable { T? ID { get; set; } } 公共接口IEntityBase,其中T:struct,IComparable { T?ID{get;set;} } 以及 公共类EntityBase:EntityBase,其中T:struct,IComparable { 私人电话号码; 受保护的EntityBase(T键) { 这个。_

我有以下代码:

public interface IEntityBase<T> where T : struct, IComparable
{
    T? ID { get; set; }
}
公共接口IEntityBase,其中T:struct,IComparable
{
T?ID{get;set;}
}
以及

公共类EntityBase:EntityBase,其中T:struct,IComparable
{
私人电话号码;
受保护的EntityBase(T键)
{
这个。_id=key;
}
公共虚拟T?Id
{
获取{返回此。\u id;}
设置{this.\u id=value;}
}
公共覆盖布尔等于(对象实体)
{
if(entity==null | |!(entity为EntityBase))
{
返回false;
}
返回(this==(EntityBase)实体);
}
公共静态布尔运算符==(EntityBase左,EntityBase右)
{
如果((对象)左==null&&(对象)右==null)
{
返回true;
}
if((对象)left==null | |(对象)right==null)
{
返回false;
}
if(left.Id.Value.CompareTo(right.Id.Value)!=0)
{
返回false;
}
返回true;
}
公共静态布尔运算符!=(EntityBase左,EntityBase右)
{
返回(!(左==右));
}
公共覆盖int GetHashCode()
{
返回此。_id.HasValue?此。_id.Value.GetHashCode():0;
}
}
同样,MyContext代码:

但在ObjectSet函数中,出现错误消息

什么是正确的语法

public class MyContext : ObjectContext
{
    private IObjectSet<Page> _pageSet;

    public MyContext(EntityConnection connection)
        : base(connection)
    {
    }

    public IObjectSet<Page> PageSet
    {
        get
        {
            return _pageSet ?? (_pageSet = ObjectSet<Page>());
        }
    }

    public virtual IObjectSet<TEntity> ObjectSet<TEntity>() where TEntity : struct, EntityBase<TEntity>, IComparable
    {
        return CreateObjectSet<TEntity>();
    }
}
公共类MyContext:ObjectContext
{
私有IObjectSet_页面集;
公共MyContext(EntityConnection连接)
:底座(连接)
{
}
公共IObjectSet页面集
{
得到
{
返回_pageSet???(_pageSet=ObjectSet());
}
}
公共虚拟IObjectSet ObjectSet(),其中tenty:struct,EntityBase,IComparable
{
返回CreateObjectSet();
}
}
班级网页:

public class Page : EntityBase<long>
{
    public Page(long? key)
        : base(key)
    {
    }

    public virtual string Title { get; set; }
    public virtual string Body { get; set; }
    public virtual Page Parent { get; set; }
}
公共类页面:EntityBase
{
公共页(长?键)
:基本(键)
{
}
公共虚拟字符串标题{get;set;}
公共虚拟字符串体{get;set;}
公共虚拟页父项{get;set;}
}

谢谢

您没有指定您将遇到的错误类型。。。请确保您正在构建目标为4.0的框架…

这一行:

public virtual IObjectSet<TEntity> ObjectSet<TEntity>() 
                where TEntity : struct, EntityBase<TEntity>, IComparable

请注意,这只是基于您的其他代码的猜测,但主要问题是您试图将泛型参数限制为结构和类,您需要解决这一问题。

您看到了什么错误消息?在MyContext.cs中,特别是在ObjectSet函数中,此消息“EntityBase”:不能同时指定约束类和“类”或“结构”约束“谢谢你,菲利普·里克!再次查看上面的代码,添加完整代码。我希望现在有一个清晰的愿景?@Abdalla-您需要从
ObjectSet
通用方法中删除
struct
约束。但是,
struct
约束可以保留在实体本身上。
public virtual IObjectSet<TEntity> ObjectSet<TEntity>() 
                where TEntity : struct, EntityBase<TEntity>, IComparable
public virtual IObjectSet<TEntity> ObjectSet<TEntity, T>() 
                where TEntity : EntityBase<T>
                where T : struct, IComparable