C
C#2y ago
Playboi17

❔ Unsupported method exception, how do I fix it?

[ApiController]
[Route($"api/[controller]")]
public class CompanyController : ControllerBase
{
private ApplicationDbContext _dbContext;
public CompanyController(ApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
[HttpPost("{id}")]
public async Task<ActionResult<TItem>> Get(string id, [FromBody] string[] includes)
{
var result = await _dbContext.Set<Company>()
.Include(x => x.Locations) // THIS CAUSES ERROR
.Where(x => x.Id == id)
.AsNoTracking()
.FirstOrDefaultAsync();
if (result == null)
{
return BadRequest(nameof(TItem) + "Id does not exist");
}
return Ok(result);
}
}


[Test]
public async Task GetTest()
{
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(databaseName: "TestDb")
.Options;
var operationalStoreOptions = Options.Create(new OperationalStoreOptions());

var mockDbContext = new Mock<ApplicationDbContext>(options, operationalStoreOptions);
var mockDbSet = new Mock<DbSet<Company>>();
mockDbContext.Setup(x => x.Set<Company>()).Returns(mockDbSet.Object);
var companyController= new CompanyController(mockDbContext.Object);
var id = "123";
var includes = new string[] { "Locations" };

// Act
var result = await companyController.Get(id, includes);

// Assert
Assert.IsInstanceOf<OkObjectResult>(result);
}
[ApiController]
[Route($"api/[controller]")]
public class CompanyController : ControllerBase
{
private ApplicationDbContext _dbContext;
public CompanyController(ApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
[HttpPost("{id}")]
public async Task<ActionResult<TItem>> Get(string id, [FromBody] string[] includes)
{
var result = await _dbContext.Set<Company>()
.Include(x => x.Locations) // THIS CAUSES ERROR
.Where(x => x.Id == id)
.AsNoTracking()
.FirstOrDefaultAsync();
if (result == null)
{
return BadRequest(nameof(TItem) + "Id does not exist");
}
return Ok(result);
}
}


[Test]
public async Task GetTest()
{
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(databaseName: "TestDb")
.Options;
var operationalStoreOptions = Options.Create(new OperationalStoreOptions());

var mockDbContext = new Mock<ApplicationDbContext>(options, operationalStoreOptions);
var mockDbSet = new Mock<DbSet<Company>>();
mockDbContext.Setup(x => x.Set<Company>()).Returns(mockDbSet.Object);
var companyController= new CompanyController(mockDbContext.Object);
var id = "123";
var includes = new string[] { "Locations" };

// Act
var result = await companyController.Get(id, includes);

// Assert
Assert.IsInstanceOf<OkObjectResult>(result);
}
But I get this error
System.NotSupportedException : Specified method is not supported.
at Microsoft.EntityFrameworkCore.DbSet`1.System.Linq.IQueryable.get_Provider()
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.Include[TEntity](IQueryable`1 source, String navigationPropertyPath)
System.NotSupportedException : Specified method is not supported.
at Microsoft.EntityFrameworkCore.DbSet`1.System.Linq.IQueryable.get_Provider()
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.Include[TEntity](IQueryable`1 source, String navigationPropertyPath)
24 Replies
Playboi17
Playboi172y ago
I dont get it, what am I supposed to do. I did a good amount of research but found no work arounds or solutions. Im guessing the method (.Include(x > x.Locations)) is not supported because its from EF Core? I don't see why thats a problem though I have that package referenced in my test project. I can't just delete my include statements from my controllers, they are very important
phaseshift
phaseshift2y ago
Are you running on .net framework or something?
Playboi17
Playboi172y ago
Yeah net7.0
phaseshift
phaseshift2y ago
So no
Playboi17
Playboi172y ago
??
phaseshift
phaseshift2y ago
Net7 isn't framework
Playboi17
Playboi172y ago
Then why does it say this <TargetFramework>net7.0</TargetFramework>
phaseshift
phaseshift2y ago
'framework' is 4.8 and under
Playboi17
Playboi172y ago
Okay
phaseshift
phaseshift2y ago
After that it's "dotnet core" or "dotnet"
Playboi17
Playboi172y ago
Knowing its net7.0, do you have a solution?
phaseshift
phaseshift2y ago
Sorry, I don't know what's up unless you're using a mis match of libs, but not sure how that would be possible What is the package reference you have?
Playboi17
Playboi172y ago
<PackageReference Include="EntityFramework" Version="6.4.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="EntityFramework" Version="6.4.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
phaseshift
phaseshift2y ago
Err, well you're mixing EF and EF Core, it looks like
Playboi17
Playboi172y ago
I can fix that, I just added that, it isn't the source of the problem
Saber
Saber2y ago
dont mock ef, its one of the stupidest wastes of time
Playboi17
Playboi172y ago
What do I do about Unit testing my controller then? Or should I just unit test the Client of the controller?
Saber
Saber2y ago
either use the inmemory context, or integration test this
Playboi17
Playboi172y ago
I did use inmemorycontext I thought?
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(databaseName: "TestDb")
.Options;
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(databaseName: "TestDb")
.Options;
Saber
Saber2y ago
you created the options, but then you use a mock for the dbcontext making that pointless
Playboi17
Playboi172y ago
So what would need changed to use an inmemorycontext
Saber
Saber2y ago
remove anything related to mocking
Playboi17
Playboi172y ago
Well Ill be damned, its working
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(databaseName: "TestDb")
.Options;
var operationalStoreOptions = Options.Create(new OperationalStoreOptions());
var dbContext = new ApplicationDbContext(options, operationalStoreOptions);
dbContext.Companies.Add(new Company()
{
Description = "tes",
Id = "1",
Industry = "music",
Locations = new List<Location>()
{
new Location()
{
Address = "asdd",
City = "asdd",
Id = "12",
LocationReviews = new List<LocationReview>(),
}
},
LogoDeleteUrl = string.Empty,
LogoUrl = String.Empty,
Name = "testname"
});
await dbContext.SaveChangesAsync();
var entityDbController = new CompanyController(dbContext);
var id = "1";
var includes = new string[] { "Locations" };

// Act
var result = await entityDbController.Get(id, includes);

// Assert
Assert.IsInstanceOf<OkObjectResult>(result);
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(databaseName: "TestDb")
.Options;
var operationalStoreOptions = Options.Create(new OperationalStoreOptions());
var dbContext = new ApplicationDbContext(options, operationalStoreOptions);
dbContext.Companies.Add(new Company()
{
Description = "tes",
Id = "1",
Industry = "music",
Locations = new List<Location>()
{
new Location()
{
Address = "asdd",
City = "asdd",
Id = "12",
LocationReviews = new List<LocationReview>(),
}
},
LogoDeleteUrl = string.Empty,
LogoUrl = String.Empty,
Name = "testname"
});
await dbContext.SaveChangesAsync();
var entityDbController = new CompanyController(dbContext);
var id = "1";
var includes = new string[] { "Locations" };

// Act
var result = await entityDbController.Get(id, includes);

// Assert
Assert.IsInstanceOf<OkObjectResult>(result);
Its goofy, Ill refactor later
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.