I am trying to create a generic repository with a generic base class.
Fo rmy base class I have;
public abstract class Entity<T> : IEntity<T>
{
public abstract T Id { get; set; }
[Column("IsArchived")]
public bool? Archived { get; set; }
}
with the interface
public interface IEntity<T>
{
T Id { get; set; }
bool? Archived { get; set; }
}
I then want to add an extension method to the Generic repository, where by it either adds or delete based on whether the T.Id is in the default state or not. My method is as follows;
public static TContext Attach<T, TKey, TContext>(this TContext context, T entity)
where T : class, IEntity<TKey>
where TContext : BaseDataContext
{
if (EqualityComparer<T>.Default.Equals(entity.Id, default(TKey)))
{
context.Set<T>().Add(entity);
}
else
{
context.Entry(entity).State = EntityState.Modified;
}
return context;
}
I then call want to call the extension method from the repository i.e.
public async Task<Entity<T>> SaveAsync<T, TKey>(Entity<T> entity, string userName) where T : class, IEntity<TKey>
{
this.Attach(entity);
await this.SaveChangesAsync();
return entity;
}
where i get the error
Error 9 Using the generic method 'Repository.Extensions.Attach(TContext, T)' requires 3 type arguments
This works fine when the base class is not a generic type, i.e. int, what o I need to adjust to fix this please?
TKeyargument, and unfortunately in such case it requires you to specify ALL generic type arguments. - Ivan Stoev