How initializes this is code with IObjectSet?
I have this code:
public interface IEntityBase<T> where T : struct, IComparable
{
T? ID { get; set; }
}
And,
p开发者_StackOverflow社区ublic class EntityBase<T> : IEntityBase<T> where T : struct, IComparable
{
private T? _id;
protected EntityBase(T? key)
{
this._id = key;
}
public virtual T? Id
{
get { return this._id; }
set { this._id = value; }
}
public override bool Equals(object entity)
{
if (entity == null || !(entity is EntityBase<T>))
{
return false;
}
return (this == (EntityBase<T>)entity);
}
public static bool operator ==(EntityBase<T> left, EntityBase<T> right)
{
if ((object)left == null && (object)right == null)
{
return true;
}
if ((object)left == null || (object)right == null)
{
return false;
}
if (left.Id.Value.CompareTo(right.Id.Value) != 0)
{
return false;
}
return true;
}
public static bool operator !=(EntityBase<T> left, EntityBase<T> right)
{
return (!(left == right));
}
public override int GetHashCode()
{
return this._id.HasValue ? this._id.Value.GetHashCode() : 0;
}
}
too, MyContext Code:
but in ObjectSet function, appearance error messages!
What is correct syntax?
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>();
}
}
class Page:
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; }
}
thanks!
With this line:
public virtual IObjectSet<TEntity> ObjectSet<TEntity>()
where TEntity : struct, EntityBase<TEntity>, IComparable
You are specifying that TEntity
must be a struct and a reference type (EntityBase<>
is a class, hence a reference type). These are mutually exclusive. What you probably want (it's hard to say without seeing CreateObjectSet) is:
public virtual IObjectSet<TEntity> ObjectSet<TEntity, T>()
where TEntity : EntityBase<T>
where T : struct, IComparable
Note that this is just a guess based on your other code, but the main issue is that you are trying to limit a generic parameter to be both a struct and a class, and you need to fix that.
You do not specify what kind of error you are getting... Be sure that you are building targeting 4.0 Framework...
精彩评论