这应该更简单,微软,嗯,不是吗?
public abstract class AbstractRepository<TContext> : IRepository
where TContext : DataServiceContext
{
protected TContext Context;
public void SaveOrUpdate<TEntity>(TEntity entity)
{
try
{
this.Context.UpdateObject(entity);
}
catch
{
try
{
this.Context.AddObject(this.ResolveEntitySet(typeof(TEntity)), entity);
}
catch
{
}
}
}
private string ResolveEntitySet(Type type)
{
var entitySetAttribute = (EntitySetAttribute)type.GetCustomAttributes(typeof(EntitySetAttribute), true).FirstOrDefault();
if (entitySetAttribute != null)
return entitySetAttribute.EntitySet;
return null;
}
}