I am trying to create some DbSet<T>
extensions where T
implements INWatchStandardEntity
.
Here is any example of one of them:
public static class NWatchDbSetExtensions
{
public static Int32 GenerateId<TEntity>(this DbSet<INWatchStandardEntity> dbSet, string name)
where TEntity : INWatchStandardEntity, new()
{
NWatchObjectType objectType = Common.ToObjectType(dbSet.GetType());
return Common.GetObjectIdFromName(objectType, name);
}
}
This compiles fine, however, when I try to use the GenerateId(string name)
on a DbSet
that inherits from INWatchStandardEntity
, for example:
using (var context = Application.GetDbContext()) {
context.Nodes.GenerateId<INWatchStandardEntity>("name");
}
I get the following message:
'
DbSet<Node>
' does not contain a definition for 'GenerateId' and the best extension method overload 'NWatchDbSetExtensions.GenerateId(DbSet<INWatchStandardEntity>
,string
)' requires a receiver of type 'DbSet<INWatchStandardEntity>
'
Is there a way to make this and other extension methods available so that I can use it against any DbSet where Entity implements INWatchStandardEntity?
After some experimentation, here is the correct way of doing this:
public static Int32 GenerateId<TEntity>(this DbSet<TEntity> dbSet, string name)
where TEntity : class, INWatchStandardEntity
{
NWatchObjectType objectType = Common.ToObjectType(dbSet.GetType());
return Common.GetObjectIdFromName(objectType, name);
}
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.