Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Declaring interface members with generic return types: "The type parameter [generic type] cannot be used with type arguments"

Post

Declaring interface members with generic return types: "The type parameter [generic type] cannot be used with type arguments"

+4
−0

I'm trying to write an interface to define the set of operations I expect my repositories' Unit of Work implementations to have, and I want this interface to be fulfilled by EF Core's DbContext class.

So far this is what I got: (I know the name is awful, suggestions accepted):

public interface IDbContextable<TContext, TEntityEntry>
    where TContext : class, IDisposable
    where TEntityEntry : class
{
    public TEntityEntry Add([NotNullAttribute] object entity);

    public TEntityEntry<TEntity> Add<TEntity>([NotNullAttribute] TEntity entity)
        where TEntity : class;

    public int SaveChanges();
}

The first Add method's signature from EF Core is: public virtual EntityEntry Add([NotNullAttribute] object entity); - and it works fine.

I'm having trouble with the second. The compiler complains:

Error CS0307 The type parameter 'TEntityEntry' cannot be used with type arguments

For completeness, my context class looks like this:

public partial class EntityFrameworkContext : DbContext, IDbContextable<EntityFrameworkContext, EntityEntry>
{
    //... some extra methods here
}

How to properly declare the interface to get rid of the error, and so that the signature: public virtual EntityEntry<TEntity> Add<TEntity>([NotNullAttribute] TEntity entity) where TEntity : class; from EF Core becomes valid?

Thanks.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (10 comments)
General comments
FoggyFinder‭ wrote over 3 years ago

Not sure I follow the Q. TEntityEntry type isn't generic. Why can't you just write public TEntityEntry Add<TEntity>([NotNullAttribute] TEntity entity) where TEntity : class;?

Marc.2377‭ wrote over 3 years ago · edited over 3 years ago

@FoggyFinder I tried, but then in my class that inherits from DbContext I get: Error CS0738 'EntityFrameworkContext' does not implement interface member 'IDbContextable<EntityFrameworkContext, EntityEntry>.Add<TEntity>(TEntity)'. 'DbContext.Add<TEntity>(TEntity)' cannot implement 'IDbContextable<EntityFrameworkContext, EntityEntry>.Add<TEntity>(TEntity)' because it does not have the matching return type of 'EntityEntry'.

Marc.2377‭ wrote over 3 years ago · edited over 3 years ago

... then the only valid implementation would be: public EntityEntry Add<TEntity>(TEntity entity) where TEntity : class - but I want the implementation that already exists in the base class (DbContext) to be valid. And its signature returns EntityEntry<TEntity>.

FoggyFinder‭ wrote over 3 years ago

ah, the goal is avoiding something like that EntityEntry IDbContextable<EntityFrameworkContext, EntityEntry>.Add<TEntity>(TEntity entity) => base.Add(entity); in the EntityFrameworkContext class?

Marc.2377‭ wrote over 3 years ago

@FoggyFinder yes, the interface must be applicable to the method already defined in the base class, which in this case is part of the framework.

Peter Taylor‭ wrote over 3 years ago

I'm confused. https://docs.microsoft.com/en-us/dotnet/api/system.data.entity.dbcontext?view=entity-framework-6.2.0 doesn't list any Add method. Is that the wrong DbContext?

FoggyFinder‭ wrote over 3 years ago

@Peter Taylor the link you provided is referenced to EF while here it's about EF Core. Correct link would be: Add

FoggyFinder‭ wrote over 3 years ago

@Marc.2377 I doubt it is possible. See Using generics in generic type constraint #3208 for details.

FoggyFinder‭ wrote over 3 years ago

btw, why do you have such restrictions? What's wrong with calling base inside of EntityFrameworkContext?

Marc.2377‭ wrote over 3 years ago · edited over 3 years ago

@FoggyFinder is that so I can enforce new user-created context types to provide the same set of methods.