Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9155453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:35:46+00:00 2026-06-17T12:35:46+00:00

I am trying to work around a mocking issue by creating a custom mock

  • 0

I am trying to work around a mocking issue by creating a custom mock of IDbSet.

The custom mock:

public class DbSetMock : IDbSet<Tenant>
{
    /* hidden all other implemented methods/properties */

    public TDerivedEntity Create<TDerivedEntity>() where TDerivedEntity : class, Tenant
    {
        throw new NotImplementedException();
    }
}

The create method gives a build error that I have no clue on how to solve:

cannot specify both a constraint class and the ‘class’ or ‘struct’ constraint

Simply removing class from the constraints results in another build error (which I also don’t understand 🙁 ).

The constraints for type parameter ‘TDerivedEntity’ of method ‘Tests.DAL.Tenants.DbSetMock.Create<TDerivedEntity>()’ must match the constraints for type parameter ‘TDerivedEntity’ of interface method ‘System.Data.Entity.IDbSet<BusinessLayer.DAL.Tenants.Tenant>.Create<TDerivedEntity>()’. Consider using an explicit interface implementation instead.

Can anybody help me successfully building this class?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:35:47+00:00Added an answer on June 17, 2026 at 12:35 pm

    Since the TDerived type parameter is constrained to be a Tenant, adding the constraints class or struct is redundant. Just remove the class constraint.

    UPDATE: Curiously there seems to be a conflict between the compiler errors here. If you “fix” one you get the other, in an infinite loop of despair. Luckily, the second error also gives us a way out: you can use an explicit interface implementation:

    public class DbSetMock : IDbSet<Tenant>
    {
    
        TDerivedEntity IDbSet<Tenant>.Create<TDerivedEntity>()
        {
            throw new NotImplementedException();
        }
    
    }
    

    There seems to be no way to implement that method without using explicit interface implementation. If you need it as part of the public interface of the class, I suggest creating another method that the interface implementation forwards to:

    public class DbSetMock : IDbSet<Tenant>
    {
    
        TDerivedEntity IDbSet<Tenant>.Create<TDerivedEntity>()
        {
            return Create<TDerivedEntity>();
        }
    
        public TDerivedEntity Create<TDerivedEntity>() where TDerivedEntity : Tenant
        {
            throw new NotImplementedException();
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a work around for this issue, but I'm trying to figure out
I'm trying to work around some logic with an issue I have regarding pagination.
I'm trying to work around a webview issue on Android devices using Select drop
In order to work around a mysql 5.0 to 5.1 compatibility issue, I'm trying
I'm trying to work around the limitation that the default ctor of a custom
I am trying to work around the issue where you can't have a form
One of the ways I'm trying to work around Flyway issue 156 ( see
I am trying to detetct/work around this bug in RSS elements. That means I
I'm still a newbie in Android and I'm trying to work my way around
Right now, I'm trying to work around an IE6/7 bug which requires the wrapping

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.