Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extension methods are not found while using Interface with DbContext #18442

Closed
sudurrani opened this issue Oct 18, 2019 · 6 comments
Closed

Extension methods are not found while using Interface with DbContext #18442

sudurrani opened this issue Oct 18, 2019 · 6 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@sudurrani
Copy link

While using an interface for entities with DbContext class can't use/found _context.Database

Steps to reproduce

Interface
public interface IMainDbContext
{
    DbSet<User> Users { get; set; }
    DbSet<Client> Clients { get; set; }
    Task<int> SaveChangesAsync(CancellationToken cancellationToken);
}
DbContext Class
public partial class MainDbContext : DbContext, IMainDbContext
{
   public MainDbContext(DbContextOptions<MainDbContext> options)
    : base(options)
   {
   }       
   public DbSet<User> Users { get; set; }
   public DbSet<Client> Clients { get; set; }
}

Created a class(RDFacadeExtensions) mentioned here 1862

Now whenever I inject IMainDbContext and use _context.Database.ExecuteNonQueryAsync is not found gives compile time error.

Further technical details

EF Core version: 3.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.0
Operating system: Windows 10
IDE: Visual Studio 2019 16.3.1

@ajcvickers
Copy link
Member

@sudurrani In the code above, the interface IMainDbContext doesn't have any Database property, so I wouldn't expect this to compile. If I'm missing something, then please post a small project/solution that demonstrates the issue so that we can be sure that we're discussing the same code.

@sudurrani
Copy link
Author

It's very simple, there is an interface with entities and this interface is implemented by a class also inherited from DbContext.
Now when ever I try to inject interface and want to write query like.

{
IMainDbContext _context;
      public Any(IMainDbContext context)
      {
           _context = context;
      }
     public List<User> AnyFunction()
     {
            var result = _context.Database.
                       // Here is the issue I can't find Database
      }       
} 

@ajcvickers
Copy link
Member

@sudurrani That's because the interface doesn't define a Database property.

@sudurrani
Copy link
Author

@ajcvickers exactly when I add Database property then whenever I use _context.Database.ExecuteSqlCommand here I won't be able to access .ExecuteSqlCommand.
Actually .Database & .ExecuteSqlCommand are DbContext builtin but here I'm using interface with my context class hence unable to get these methods.
Still Can't In .Database & .ExecuteSqlCommand into the game.

@ajcvickers
Copy link
Member

@sudurrani Unless I'm missing something, that's just the way C#/.NET works. Is there something EF should be doing differently here?

@sudurrani
Copy link
Author

I found the solution.
Therefore closing the issue.

@ajcvickers ajcvickers added closed-no-further-action The issue is closed and no further action is planned. and removed type-bug labels Oct 28, 2019
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

2 participants