I've got multiple data context, they require the same extension method. What would be the ideal way in this class, to make it more generic for the return type?
public static class DBContextExtensions
{
public static TContext Attach<TEntity, TContext>(this TContext context, TEntity entity) where TEntity : class, IEntity
{
context.Entry(entity).State = entity.Id == 0 ? EntityState.Added : EntityState.Modified;
return context;
}
}
It doesn't appear to resolve the Entry
method in Entity Framework. How could I expose the method via a generic?
Greg,
Add another generic constraint:
public static TContext Attach<TEntity, TContext>(this TContext context, TEntity entity)
where TEntity : class, IEntity
where TContext : DbContext // <----- add this constraint
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.